英文:
Not able to migrate, Foreignkey in models is causing the issue
问题
It seems like you're encountering an issue with the to_field
argument in your Django models. The error message you provided indicates that the to_field
value for the foreign key in your Team
model doesn't exist on the related Organisation
model.
In your Team
model, you have this line:
org_id = models.ForeignKey(Organisation(), to_field=id, on_delete=models.CASCADE)
It appears that you want to link Team.org_id
to Organisation.id
, but the correct way to specify the to_field
argument is by using a string with the field name enclosed in quotes, like this:
org_id = models.ForeignKey(Organisation, to_field='id', on_delete=models.CASCADE)
Make sure to use quotes around 'id'
to indicate that you want to use the id
field of the Organisation
model as the target field for the foreign key.
After making this change, try running the migration again to see if the issue is resolved.
英文:
so I have the two following models that I'm trying to migrate.
class Organisation(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=256)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=64,choices=STATUS,default='active')
logo_file_path = models.CharField(max_length=2048)
and
class Team(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=256)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
status = models.CharField(max_length=64,choices=STATUS,default='active')
org_id = models.ForeignKey(Organisation(),to_field=id, on_delete=models.CASCADE)
I need Team.org_id linked to Organisation.id, so I'm trying to use it as a foreign key.
but while trying to make migration it is giving me the following system error:
SystemCheckError: System check identified some issues:
ERRORS:
db_app.Team.org_id: (fields.E312) The to_field 'db_app.Team.id' doesn't exist on the related model 'db_app.Organisation'.
Note: db_app is my django app name.
I guess the to_field
argument in the foreign key is causing the issue. I'm not sure though.
I have tried it on different models in a different project the error remains the same.
答案1
得分: 1
尝试这个:
org_id = models.ForeignKey(Organisation, to_field=id, on_delete=models.CASCADE)
或者
org_id = models.ForeignKey(Organisation, on_delete=models.CASCADE)
还有,Organization类是在Team类之前创建的。
英文:
try this:
org_id = models.ForeignKey(Organisation,to_field=id, on_delete=models.CASCADE)
or
org_id = models.ForeignKey(Organisation, on_delete=models.CASCADE)
And also the Organization class is created above the Team class
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论