该字段没有指定默认值,但不是NULL

时间:2022-11-15 00:05:10

I have no idea what Django is trying to tell me. I have a model, WeekTwo, which inherits from Week, which inherits from modelsModel. I have another model, UserProfile. I want to use WeekTwo as a OneToOne Key in UserProfile, so I inserted the following line of code:

我不知道姜戈想告诉我什么。我有一个模型,week2,它继承自Week,它继承自modelsModel。我有另一个模型,UserProfile。我想使用week2作为UserProfile中的一个toone键,所以我插入了以下代码行:

weekTwo = models.OneToOneField(WeekTwo)

However, when I try to migrate my database using python manage.py schemamigration my_app --auto, I get the following error:

但是,当我尝试使用python管理迁移数据库时。py schemamigration my_app—auto,我得到如下错误:

The field 'UserProfile.weekTwo' does not have a default specified, yet is NOT NULL.

I tried adding default=0 to my weekTwo declaration, but now I'm getting this error when I try the schema migration:

我尝试在我的第2周声明中添加default=0,但是现在我在尝试模式迁移时得到了这个错误:

IntegrityError: column weekTwo_id is not unique

Moreover, south is now telling me that I am in an interim state between migrations and that I might be able to recover. I literally have no idea what any of this means.

此外,南方现在告诉我,我处于移民之间的过渡状态,我可能会康复。我真的不知道这是什么意思。

1 个解决方案

#1


3  

Be aware before going further, be sure that if South already did any migration that has been failed, It's better to redo|recover to the last migration that was working.

在继续深入之前,请注意,如果South已经做了任何失败的迁移,那么最好将|恢复到正在工作的最后一次迁移。

You have two options here, at first you can do Data Migration. Look at ref also.

这里有两个选项,首先可以进行数据迁移。看看裁判。

In Second way You can make weekTwo field null and blank first

第二种方法是让weekTwo字段首先为空

weekTwo = models.OneToOneField(WeekTwo, null=True, blank=True)

Then let South generate a migration for you by

然后让南方为你生成迁移

python manage.py schemamigration my_app --auto

I'm sure South won't complain about it now, Then

我相信现在南方不会再抱怨了

python manage.py migrate

If everything is okay now, You can now get back and change to weekTwo field to

如果现在一切正常,您现在可以返回并切换到week2字段

weekTwo = models.OneToOneField(WeekTwo)

And generate migration then migrate them.

然后进行迁移。

Anyway, When south find out your field in not NULL and doesn't have a default value, at the schemamigration step it will suggest you to provide a value for it, Again here your field is OneToOneField, because even south gives you chance to provide a default value for your existed record on model again uniqueness of weekTwo field will raise an error.

无论如何,当南找出你的字段非空和没有一个默认值,在schemamigration一步将建议你为它提供一个值,这里你的领域是一对一字段,因为即使南给你机会为您的存在提供一个默认值记录模型再次weekTwo领域的独特性将会提高一个错误。

I think still you have to go with Data Migration if second way didn't work, Or gives it a shot and try the second way this time instead of make it null, blank change the whole Field type. Try it with;

我认为,如果第二种方法不起作用,你仍然需要进行数据迁移,或者尝试第二种方法,而不是让它为空,空白改变整个字段类型。试一试;

weekTwo = models.ForeignKey(WeekTwo)

But keep in mind Data migration would be definitely smarter and standard way here.

但是请记住,数据迁移肯定是更聪明、更标准的方式。

#1


3  

Be aware before going further, be sure that if South already did any migration that has been failed, It's better to redo|recover to the last migration that was working.

在继续深入之前,请注意,如果South已经做了任何失败的迁移,那么最好将|恢复到正在工作的最后一次迁移。

You have two options here, at first you can do Data Migration. Look at ref also.

这里有两个选项,首先可以进行数据迁移。看看裁判。

In Second way You can make weekTwo field null and blank first

第二种方法是让weekTwo字段首先为空

weekTwo = models.OneToOneField(WeekTwo, null=True, blank=True)

Then let South generate a migration for you by

然后让南方为你生成迁移

python manage.py schemamigration my_app --auto

I'm sure South won't complain about it now, Then

我相信现在南方不会再抱怨了

python manage.py migrate

If everything is okay now, You can now get back and change to weekTwo field to

如果现在一切正常,您现在可以返回并切换到week2字段

weekTwo = models.OneToOneField(WeekTwo)

And generate migration then migrate them.

然后进行迁移。

Anyway, When south find out your field in not NULL and doesn't have a default value, at the schemamigration step it will suggest you to provide a value for it, Again here your field is OneToOneField, because even south gives you chance to provide a default value for your existed record on model again uniqueness of weekTwo field will raise an error.

无论如何,当南找出你的字段非空和没有一个默认值,在schemamigration一步将建议你为它提供一个值,这里你的领域是一对一字段,因为即使南给你机会为您的存在提供一个默认值记录模型再次weekTwo领域的独特性将会提高一个错误。

I think still you have to go with Data Migration if second way didn't work, Or gives it a shot and try the second way this time instead of make it null, blank change the whole Field type. Try it with;

我认为,如果第二种方法不起作用,你仍然需要进行数据迁移,或者尝试第二种方法,而不是让它为空,空白改变整个字段类型。试一试;

weekTwo = models.ForeignKey(WeekTwo)

But keep in mind Data migration would be definitely smarter and standard way here.

但是请记住,数据迁移肯定是更聪明、更标准的方式。