MySQL gives an “Unknown column 'user.id' in 'field list'” error using Django's automatic id

前端 未结 2 1986
一整个雨季
一整个雨季 2021-01-26 11:23

I have my User model set up with no primary key so that the automatic id will be used instead. However, when I try to access it using Django\'s \"

相关标签:
2条回答
  • 2021-01-26 11:46

    Not 100% sure, but I think even though Django will add the id field to the model class, that field will not propagate to the DB with syncdb.

    One way to try it would be to rename the existing User table, run syncdb and see if the User table is created. If not (which is likely because of the managed flag) try again with managed=True. If the id field appears in this case then my guess is you'll have to add it manually to the User table with the same parameters as the automatically created one.

    0 讨论(0)
  • 2021-01-26 12:04

    EDIT : wrong answer. check comments below

    The problem is that you have managed set to False. According to the documentation

    If False, no database table creation or deletion operations will be performed for this model. This is useful if the model represents an existing table or a database view that has been created by some other means. This is the only difference when managed=False. All other aspects of model handling are exactly the same as normal. This includes

    Adding an automatic primary key field to the model if you don’t declare it. To avoid confusion for later code readers, it’s recommended to specify all the columns from the database table you are modeling when using unmanaged models.

    You will need to define the primary key as this is not done by default anymore.

    0 讨论(0)
提交回复
热议问题