运行迁移时"Unique constrainted failed"问题



我一直试图通过应用和运行迁移来更改django模型,但我一直收到这个错误django.db.utils.IntegrityError: UNIQUE constraint failed: new__startup_home_mates.user_id如何修复这个错误?我已经调查了很多,但没有发现任何适合我的情况。我已经尝试过直接从迁移文件中使用RenameField和AlterField,但它仍然不起作用——也许我做错了。

型号.py

class Mates(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='usermates', unique=True)
users_requests = models.ManyToManyField(User, related_name="users_requests")
req_bio = models.CharField(max_length=400)
req_image = models.ImageField(upload_to='requestmates_pics', null=True, blank=True, default=False)

回溯

Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "C:UsersUSEREnvsstartuplibsite-packagesdjangocoremanagement__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:UsersUSEREnvsstartuplibsite-packagesdjangocoremanagement__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "C:UsersUSEREnvsstartuplibsite-packagesdjangocoremanagementbase.py", line 328, in run_from_argv
self.execute(*args, **cmd_options)
File "C:UsersUSEREnvsstartuplibsite-packagesdjangocoremanagementbase.py", line 369, in execute
output = self.handle(*args, **options)
File "C:UsersUSEREnvsstartuplibsite-packagesdjangocoremanagementbase.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "C:UsersUSEREnvsstartuplibsite-packagesdjangocoremanagementcommandsmigrate.py", line 231, in handle
post_migrate_state = executor.migrate(
File "C:UsersUSEREnvsstartuplibsite-packagesdjangodbmigrationsexecutor.py", line 117, in migrate
state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
File "C:UsersUSEREnvsstartuplibsite-packagesdjangodbmigrationsexecutor.py", line 147, in _migrate_all_forwards
state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
File "C:UsersUSEREnvsstartuplibsite-packagesdjangodbmigrationsexecutor.py", line 245, in apply_migration
state = migration.apply(state, schema_editor)
File "C:UsersUSEREnvsstartuplibsite-packagesdjangodbmigrationsmigration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "C:UsersUSEREnvsstartuplibsite-packagesdjangodbmigrationsoperationsfields.py", line 249, in database_forwards
schema_editor.alter_field(from_model, from_field, to_field)
File "C:UsersUSEREnvsstartuplibsite-packagesdjangodbbackendssqlite3schema.py", line 138, in alter_field
super().alter_field(model, old_field, new_field, strict=strict)
File "C:UsersUSEREnvsstartuplibsite-packagesdjangodbbackendsbaseschema.py", line 564, in alter_field
self._alter_field(model, old_field, new_field, old_type, new_type,
File "C:UsersUSEREnvsstartuplibsite-packagesdjangodbbackendssqlite3schema.py", line 360, in _alter_field
self._remake_table(model, alter_field=(old_field, new_field))
File "C:UsersUSEREnvsstartuplibsite-packagesdjangodbbackendssqlite3schema.py", line 283, in _remake_table
self.execute("INSERT INTO %s (%s) SELECT %s FROM %s" % (
File "C:UsersUSEREnvsstartuplibsite-packagesdjangodbbackendsbaseschema.py", line 142, in execute
cursor.execute(sql, params)
File "C:UsersUSEREnvsstartuplibsite-packagesdjangodbbackendsutils.py", line 100, in execute
return super().execute(sql, params)
File "C:UsersUSEREnvsstartuplibsite-packagesdjangodbbackendsutils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "C:UsersUSEREnvsstartuplibsite-packagesdjangodbbackendsutils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "C:UsersUSEREnvsstartuplibsite-packagesdjangodbbackendsutils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "C:UsersUSEREnvsstartuplibsite-packagesdjangodbutils.py", line 90, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "C:UsersUSEREnvsstartuplibsite-packagesdjangodbbackendsutils.py", line 86, in _execute
return self.cursor.execute(sql, params)
File "C:UsersUSEREnvsstartuplibsite-packagesdjangodbbackendssqlite3base.py", line 396, in execute
return Database.Cursor.execute(self, query, params)
django.db.utils.IntegrityError: UNIQUE constraint failed: new__startup_home_mates.user_id

如果你需要查看更多代码,请告诉我:(

您可以稍后在模型中添加主键/id,但以前的数据库没有。

如果向模型添加新功能,请添加default='something'null=True,blank=True作为参数。因此,旧数据库将不需要唯一的约束/值

我怀疑您的问题出在OnetoOneFieldunique设置为True。您应该将其更改为ForeignKey(并且不要设置Unique = True(。这样用户就可以有多个伴侣。如果我正确理解了你的模型设置和你想要实现的目标。

以下是对OnetoOneFieldForeignKey之间差异的总结:什么';django OneToOneField和ForeignKey之间的区别是什么?

相关内容

  • 没有找到相关文章