gpt4 book ai didi

python - 表有多个主键 Django

转载 作者:太空宇宙 更新时间:2023-11-03 21:33:50 25 4
gpt4 key购买 nike

我有错误。首先,当我创建模型时,我犯了错误,并为模型中的所有 id 字段提供了 pk。然后我做出正确的修改并更改为唯一,但是当我 makemigrations 时出现错误,我尝试删除数据库,尝试将 venv 更改为新的,但它对这个错误没有任何作用,我想我做了代码错误,但找不到。

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

最佳答案

不需要为每个模型创建主键,因为默认情况下创建模型时,django 会为每个模型创建主键。

class Company(models.Model):
company_name = models.CharField(max_length=255, null=True)

在公司模型 django 中默认创建主键。

关于python - 表有多个主键 Django,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53362241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com