Vote count:
0
I am building a project where I have two SQL tables: wikispy_edit and wikispy_rdns. wikispy_edit contains - among other things - an ip column with an index, which is the same data type as ip column in wikispy.rdns, which is a primary key there. The problem is that I cannot connect these two with django.db.models.ForeignKey, because some of the wikispy_edit ip values do not have a corresponding value in wikispy_rdns. While Django doesn't seem to complain during ./manage.py migrate, it doesn't allow me to insert such objects using its ORM, which is annoying.
Is there a way I could model this relationship and still achieve the effect of Edit.objects.select_related().filter(rdns__rdns__somequery="blah")? I need a similar syntax because this is the only I found that allows me to perform this query in a single SQL statement and this sometimes pulls thousands of objects. If possible, I would prefer to avoid raw SQL queries.
Here's what I used so far:
class Edit(models.Model):
wikipedia_id = models.IntegerField()
title = models.CharField(max_length=1024)
wiki = models.ForeignKey('Wiki')
rdns = models.ForeignKey('RDNS', db_column='ip')
class RDNS(models.Model):
ip = models.GenericIPAddressField(primary_key=True)
rdns = models.CharField(max_length=253)
This worked well, but the problem is that this way I cannot insert a new Edit that has an IP not mentioned in wikispy_rdns. How could I work around either the edition or querying problem?
Join a table without creating a foreign key or add objects that break foreign key relationship?
Aucun commentaire:
Enregistrer un commentaire