关于django unique 的问题

关于django unique 的问题

代码如下,

class Batch(models.Model):

"""批信息"""

batch = models.CharField(max_length=20,verbose_name=u"批信息")

remark = models.CharField(max_length=40, verbose_name=u"备注",null=True,blank=True)

现在我想给batch 加一个unique=True

Operations to perform:

Apply all migrations: admin, auth, contenttypes, packagingWorkshop, qc, sessions, users

Running migrations:

Applying packagingWorkshop.0013_auto_20200416_1138...Traceback (most recent call last):

File "D:\python_demo\manhour\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute

return self.cursor.execute(sql, params)

File "D:\python_demo\manhour\venv\lib\site-packages\django\db\backends\mysql\base.py", line 71, in execute

return self.cursor.execute(query, args)

File "D:\python_demo\manhour\venv\lib\site-packages\MySQLdb\cursors.py", line 209, in execute

res = self._query(query)

File "D:\python_demo\manhour\venv\lib\site-packages\MySQLdb\cursors.py", line 315, in _query

db.query(q)

File "D:\python_demo\manhour\venv\lib\site-packages\MySQLdb\connections.py", line 239, in query

_mysql.connection.query(self, query)

MySQLdb._exceptions.OperationalError: (1068, 'Multiple primary key defined')

会报错,说是有多个主键,因为之前没定义主键,所以系统自动生成一个id给我。

尝试过,直接加unique=True ,以及primary_key=True
但是在migrate(makemiragtions没问题)都会有类似的报错

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 "D:\python_demo\manhour\venv\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line

utility.execute()

File "D:\python_demo\manhour\venv\lib\site-packages\django\core\management\__init__.py", line 375, in execute

self.fetch_command(subcommand).run_from_argv(self.argv)

File "D:\python_demo\manhour\venv\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv

self.execute(*args, **cmd_options)

File "D:\python_demo\manhour\venv\lib\site-packages\django\core\management\base.py", line 364, in execute

output = self.handle(*args, **options)

File "D:\python_demo\manhour\venv\lib\site-packages\django\core\management\base.py", line 83, in wrapped

res = handle_func(*args, **kwargs)

File "D:\python_demo\manhour\venv\lib\site-packages\django\core\management\commands\migrate.py", line 234, in handle

fake_initial=fake_initial,

File "D:\python_demo\manhour\venv\lib\site-packages\django\db\migrations\executor.py", line 117, in migrate

state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)

File "D:\python_demo\manhour\venv\lib\site-packages\django\db\migrations\executor.py", line 147, in _migrate_all_forwards

state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)

File "D:\python_demo\manhour\venv\lib\site-packages\django\db\migrations\executor.py", line 245, in apply_migration

state = migration.apply(state, schema_editor)

File "D:\python_demo\manhour\venv\lib\site-packages\django\db\migrations\migration.py", line 124, in apply

operation.database_forwards(self.app_label, schema_editor, old_state, project_state)

File "D:\python_demo\manhour\venv\lib\site-packages\django\db\migrations\operations\fields.py", line 249, in database_forwards

schema_editor.alter_field(from_model, from_field, to_field)

File "D:\python_demo\manhour\venv\lib\site-packages\django\db\backends\base\schema.py", line 535, in alter_field

old_db_params, new_db_params, strict)

File "D:\python_demo\manhour\venv\lib\site-packages\django\db\backends\base\schema.py", line 715, in _alter_field

self.execute(self._create_unique_sql(model, [new_field.column]))

File "D:\python_demo\manhour\venv\lib\site-packages\django\db\backends\base\schema.py", line 137, in execute

cursor.execute(sql, params)

File "D:\python_demo\manhour\venv\lib\site-packages\django\db\backends\utils.py", line 99, in execute

return super().execute(sql, params)

File "D:\python_demo\manhour\venv\lib\site-packages\django\db\backends\utils.py", line 67, in execute

return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)

File "D:\python_demo\manhour\venv\lib\site-packages\django\db\backends\utils.py", line 76, in _execute_with_wrappers

return executor(sql, params, many, context)

File "D:\python_demo\manhour\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute

return self.cursor.execute(sql, params)

File "D:\python_demo\manhour\venv\lib\site-packages\django\db\utils.py", line 89, in __exit__

raise dj_exc_value.with_traceback(traceback) from exc_value

File "D:\python_demo\manhour\venv\lib\site-packages\django\db\backends\utils.py", line 84, in _execute

return self.cursor.execute(sql, params)

File "D:\python_demo\manhour\venv\lib\site-packages\django\db\backends\mysql\base.py", line 71, in execute

return self.cursor.execute(query, args)

File "D:\python_demo\manhour\venv\lib\site-packages\MySQLdb\cursors.py", line 209, in execute

res = self._query(query)

File "D:\python_demo\manhour\venv\lib\site-packages\MySQLdb\cursors.py", line 315, in _query

db.query(q)

File "D:\python_demo\manhour\venv\lib\site-packages\MySQLdb\connections.py", line 239, in query

_mysql.connection.query(self, query)

django.db.utils.OperationalError: (1061, "Duplicate key name 'packagingWorkshop_batch_batch_58b8975d_uniq'")

我的问题是,这种情况下,要如何给batch字段变成主键且唯一,要如何操作呢。
除了重新删表建库。。


回答:

找到逐渐的字段名,先

alter table 表名  drop primary key;

再往batch中添加主键

alter table 表名 add constraint pk_batch primary key(batch);

思路大概是这样子,先删掉主键约束,再添加batch的主键约束,django不太懂,应该model里面有对应的方法或者你直接到存储该表的数据库在命令行操作。

以上是 关于django unique 的问题 的全部内容, 来源链接: utcz.com/a/159219.html

回到顶部