sqlite3.OperationalError: no such column: **

最近在《phthon 从入门到实战》在第19章第三部分,按照要求在models.py(learning_logs app 里面的),添加了一行:owner=models.ForeignKey(User,on_delete=models.CASCADE),即添加了用户的一个外键,在执行了makemigrations 后,sunsever 时出现了一个错误,如下:

   

无对应列owner_id,出现这个错误的原因是makemigrations 是告诉数据库添加了新列,但没有将改动应用于数据库,migrate则是将改动应用于数据库,关于makemigrations 和migrate 的区别,以下是摘自Moderator Chris Freeman的回答

"It is necessary to run both the commands to complete the migration of the database tables to be in sync with your models.

makemigrations simply analyzes your current models for any changes that would be out of sync with your database and creates a migrations file that can be used to bring the in sync. If left at this point, your models would still be out of sync with your database possibly breaking your code that queries the database.

migrate is the command to "Make It So!" and apply the changes noted during the makemigrations phase.

Applying [app] to either of these commands simply adds focus to that "app" and doesn't inspect all of your apps."

所以是忘了执行“migrate”,执行后错误消失。makemigrations、migrate 都是必要的。

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值