我有错误。首先,当我创建模型时,我会犯错并为模型中的所有id
字段提供pk。然后我做对了,并更改为唯一,但是当我makemigrations
遇到错误时,我尝试删除数据库,尝试将venv更改为new,但是此错误没有任何作用,我认为我在代码中犯了错误,但找不到它。
models.py代码:
class MainContact(models.Model):
main_contact_id = models.IntegerField(null=True, unique=True)
class Company(models.Model):
company_id = models.IntegerField(null=True, unique=True)
company_name = models.CharField(max_length=255, null=True)
class Tags(models.Model):
tags_id = models.IntegerField(null=True, unique=True)
tags_name = models.CharField(max_length=255, null=True)
class CustomFiledsValues(models.Model):
custom_fields_values_value = models.CharField(max_length=255, null=True)
custom_fields_values_enum = models.CharField(max_length=255, null=True)
custom_fields_values_subtype = models.CharField(max_length=255, null=True)
class CustomFields(models.Model):
custom_fields_id = models.IntegerField(null=True, unique=True)
custom_fields_name = models.CharField(max_length=255, null=True)
custom_fields_values = models.ForeignKey(CustomFiledsValues, on_delete = models.CASCADE)
custom_fields_is_system = models.BooleanField()
class Contacts(models.Model):
contact_id = models.IntegerField(null=True, unique=True)
class Pipeline(models.Model):
pipeline_id = models.IntegerField(null=True, unique=True)
class Deal(models.Model):
deal_id = models.IntegerField(null=True, unique=True)
name = models.CharField(max_length=255, null=True)
responsible_user_id = models.IntegerField(null=True)
created_by = models.IntegerField(null=True)
created_at = models.DateTimeField()
updated_at = models.DateTimeField()
account_id = models.IntegerField(null=True)
is_deleted = models.BooleanField()
main_contact = models.ForeignKey(MainContact, on_delete=models.CASCADE)
group_id = models.IntegerField(null=True)
company = models.ForeignKey(Company, on_delete=models.CASCADE)
closed_at = models.DateTimeField()
closest_task_at = models.DateTimeField()
tags = models.ForeignKey(Tags, on_delete=models.CASCADE)
custom_fields = models.ForeignKey(CustomFields, on_delete=models.CASCADE)
contacts = models.ForeignKey(Contacts, on_delete=models.CASCADE)
status_id = models.IntegerField(null=True)
sale = models.DecimalField(max_digits=19, decimal_places=2)
pipeline = models.ForeignKey(Pipeline, on_delete=models.CASCADE)
跟踪:
Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/home/megagun/Projects/venvs/erpmodtest/lib/python3.5/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/home/megagun/Projects/venvs/erpmodtest/lib/python3.5/site-packages/django/core/management/__init__.py", line 365, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/megagun/Projects/venvs/erpmodtest/lib/python3.5/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/megagun/Projects/venvs/erpmodtest/lib/python3.5/site-packages/django/core/management/base.py", line 335, in execute
output = self.handle(*args, **options)
File "/home/megagun/Projects/venvs/erpmodtest/lib/python3.5/site-packages/django/core/management/commands/migrate.py", line 200, in handle
fake_initial=fake_initial,
File "/home/megagun/Projects/venvs/erpmodtest/lib/python3.5/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 "/home/megagun/Projects/venvs/erpmodtest/lib/python3.5/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 "/home/megagun/Projects/venvs/erpmodtest/lib/python3.5/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/megagun/Projects/venvs/erpmodtest/lib/python3.5/site-packages/django/db/migrations/migration.py", line 122, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/home/megagun/Projects/venvs/erpmodtest/lib/python3.5/site-packages/django/db/migrations/operations/models.py", line 92, in database_forwards
schema_editor.create_model(model)
File "/home/megagun/Projects/venvs/erpmodtest/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 298, in create_model
self.execute(sql, params or None)
File "/home/megagun/Projects/venvs/erpmodtest/lib/python3.5/site-packages/django/db/backends/base/schema.py", line 117, in execute
cursor.execute(sql, params)
File "/home/megagun/Projects/venvs/erpmodtest/lib/python3.5/site-packages/django/db/backends/utils.py", line 100, in execute
return super().execute(sql, params)
File "/home/megagun/Projects/venvs/erpmodtest/lib/python3.5/site-packages/django/db/backends/utils.py", line 68, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/home/megagun/Projects/venvs/erpmodtest/lib/python3.5/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/megagun/Projects/venvs/erpmodtest/lib/python3.5/site-packages/django/db/backends/utils.py", line 85, in _execute
return self.cursor.execute(sql, params)
File "/home/megagun/Projects/venvs/erpmodtest/lib/python3.5/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/megagun/Projects/venvs/erpmodtest/lib/python3.5/site-packages/django/db/backends/utils.py", line 83, in _execute
return self.cursor.execute(sql)
File "/home/megagun/Projects/venvs/erpmodtest/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line 301, in execute
return Database.Cursor.execute(self, query)
django.db.utils.OperationalError: table "amocrmapi_deal" has more than one primary key
答案 0 :(得分:0)
不需要为每个模型创建主键,因为默认情况下,在创建模型时,django为每个模型创建主键。
class Company(models.Model):
company_name = models.CharField(max_length=255, null=True)
在公司模型django中,默认情况下创建主键。