gpt4 book ai didi

python-3.x - Django addConstraints 在 Postgres 上引发 TypeError

转载 作者:行者123 更新时间:2023-12-04 09:35:07 24 4
gpt4 key购买 nike

我在 models.py 中有一个名为 FlashNews 的模型,它可以与 Race、League 或 FantasyTeam 相关联。我使用了 Django 的 CheckConstraint(版本 2.2.1)。还有一个 type 列来确保一致性。

我想保证 racefteamleague 中只有一列不为 null,与 type 一致 值。

示例代码如下:

FlashNewsTypes = [
(1, 'Race'),
(2, 'League'),
(3, 'Fteam')
]


class FlashNews(models.Model):

race = models.ForeignKey(Race, on_delete=models.CASCADE, null=True, blank=True)
league = models.ForeignKey(League, on_delete=models.CASCADE, null=True, blank=True)
fteam = models.ForeignKey(FantasyTeam, on_delete=models.CASCADE, null=True, blank=True)
type = models.IntegerField(choices=FlashNewsTypes, default=FlashNewsTypes[0][0])

class Meta:
constraints = [
models.CheckConstraint(
name="unique_notnull_field",
check=(
models.Q(
type=FlashNewsTypes[0][0],
race__isnull=False,
league__isnull=True,
fteam__isnull=True,
) | models.Q(
type=FlashNewsTypes[1][0],
race__isnull=True,
league__isnull=False,
fteam__isnull=True,
) | models.Q(
type=FlashNewsTypes[2][0],
race__isnull=True,
league__isnull=True,
fteam__isnull=False,
)
),
)
]

自动创建的迁移:

class Migration(migrations.Migration):

dependencies = [
('myapp', '0055_flashnews'),
]

operations = [
migrations.AddConstraint(
model_name='flashnews',
constraint=models.CheckConstraint(check=models.Q(models.Q(('fteam__isnull', True), ('league__isnull', True), ('race__isnull', False), ('type', 1)), models.Q(('fteam__isnull', False), ('league__isnull', True), ('race__isnull', True), ('type', 2)), models.Q(('fteam__isnull', True), ('league__isnull', False), ('race__isnull', True), ('type', 3)), _connector='OR'), name='%(app_label)s_%(class)s_value_matches_type'),
),
]

此迁移在我的开发环境中成功,它使用 Sqlite 后端。但是,当使用 Postgres 后端执行时,它会引发此错误:

Applying myapp.0056_auto_20200502_1754...Traceback (most recent call last):
File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
File "/home/src/myapp/env/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
utility.execute()
File "/home/src/myapp/env/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/src/myapp/env/lib/python3.6/site-packages/django/core/management/base.py", line 323, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/src/myapp/env/lib/python3.6/site-packages/django/core/management/base.py", line 364, in execute
output = self.handle(*args, **options)
File "/home/src/myapp/env/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/home/src/myapp/env/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 234, in handle
fake_initial=fake_initial,
File "/home/src/myapp/env/lib/python3.6/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/src/myapp/env/lib/python3.6/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/src/myapp/env/lib/python3.6/site-packages/django/db/migrations/executor.py", line 245, in apply_migration
state = migration.apply(state, schema_editor)
File "/home/src/myapp/env/lib/python3.6/site-packages/django/db/migrations/migration.py", line 124, in apply
operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
File "/home/src/myapp/env/lib/python3.6/site-packages/django/db/migrations/operations/models.py", line 827, in database_forwards
schema_editor.add_constraint(model, self.constraint)
File "/home/src/myapp/env/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 345, in add_constraint
self.execute(sql)
File "/home/src/myapp/env/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 137, in execute
cursor.execute(sql, params)
File "/home/src/myapp/env/lib/python3.6/site-packages/django/db/backends/utils.py", line 67, in execute
return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
File "/home/src/myapp/env/lib/python3.6/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/src/myapp/env/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
return self.cursor.execute(sql, params)
TypeError: tuple indices must be integers or slices, not str

关于 Django 的约束,我在这里缺少一些特定于数据库后端的概念吗?

感谢您的帮助!

最佳答案

事实证明,为了清晰度。

但似乎这个名称是 TypeError 的原因。替换名称即可解决问题。

关于python-3.x - Django addConstraints 在 Postgres 上引发 TypeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62628292/

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