将更改迁移到模型时出现 South 错误

发布于 2024-10-29 08:48:09 字数 610 浏览 7 评论 0原文

我已将用户的外键添加到模型中,但是当我尝试使用 South 迁移数据库时,出现以下错误:

_mysql_exceptions.OperationalError: (1067, "Invalid default value for 'user_id'")

South 告诉我,我可以通过运行 SQL 命令从错误中恢复:

! You *might* be able to recover with:   = 
    ALTER TABLE `main_deal` DROP COLUMN `user_id` CASCADE; []

I尝试运行 SQL 命令,但出现以下错误:

#1091 - Can't DROP 'user_id'; check that column/key exists

我是否需要将“user_id”添加到我的模型并将默认值设置为 1?

编辑: 以下是我添加到模型中以导致发生这种情况的代码:

user = models.ForeignKey(User)

然后我使用 South 迁移了模型。

I have added a foreign key to a user to a model, but when I try to migrate the database using South, I get the following error:

_mysql_exceptions.OperationalError: (1067, "Invalid default value for 'user_id'")

South tells me that I may be able to recover from the error by running an SQL command:

! You *might* be able to recover with:   = 
    ALTER TABLE `main_deal` DROP COLUMN `user_id` CASCADE; []

I tried to run the SQL command, but I get the following error:

#1091 - Can't DROP 'user_id'; check that column/key exists

Do I need to add 'user_id' to my model and set the default value to 1?

Edit:
Here is the code that I added to my model to cause this to occur:

user = models.ForeignKey(User)

Then I migrated the model using South.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

农村范ル 2024-11-05 08:48:09

当您运行 schemamigration 命令时,系统应该会询问您一些有关默认值的问题。如果您给出了错误的答案,则可能导致了您所描述的问题。

如果您可以打开实际的迁移文件并获取顶部附近的 forward 方法,并将其发布到此处,我应该能够找出问题所在。

When you ran the schemamigration command, you should've been asked some questions about default values. If you gave the wrong answer it may have caused the problem you describe.

If you could open up the actual migration file and grab the forward method near the top, and post it here, I should be able to figure out what went wrong.

风铃鹿 2024-11-05 08:48:09

我猜你有问题,因为你指定了不可为空的用户字段而不提供默认值。您需要提供默认值,例如。 user = models.ForeignKey(User, default=foo) 或允许用户字段为空 user = models.ForeignKey(User, null=True)

顺便说一句,我认为最新South 版本在创建迁移时要求默认值。您使用哪个版本?

I guess you have a problem because you specified not nullable user field without providing default value. You need to either provide default value eg. user = models.ForeignKey(User, default=foo) or allow nulls for user field user = models.ForeignKey(User, null=True)

Btw, I think that latest version of south asks for default values when migration is being created. Which version do you use?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文