2

我有这样的约束

CheckConstraint(
    check=Q(full_path__endswith=Concat(Value("/"), F("identifier"), Value("/")))  #
    | Q(full_path="/"),
    name="path_endswith_identifier_if_not_root",
 ),

Makemigrations 运行良好,但是当我尝试实际迁移生成的迁移时,我得到IndexError: tuple index out of range

...
  File "/opt/virtual_env/mch2/lib/python3.7/site-packages/django/db/backends/utils.py", line 99, in execute
    return super().execute(sql, params)
  File "/opt/virtual_env/mch2/lib/python3.7/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/opt/virtual_env/mch2/lib/python3.7/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/opt/virtual_env/mch2/lib/python3.7/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
IndexError: tuple index out of range

当我为生成的迁移运行 sqlmigrations 时,我收到以下错误TypeError: not enough arguments for format string

这是完整的错误消息:

 ...
  File "/opt/virtual_env/mch2/lib/python3.7/site-packages/django/db/backends/base/schema.py", line 345, in add_constraint
    self.execute(sql)
  File "/opt/virtual_env/mch2/lib/python3.7/site-packages/django/db/backends/base/schema.py", line 132, in execute
    self.collected_sql.append((sql % tuple(map(self.quote_value, params))) + ending)
TypeError: not enough arguments for format string

django 在这里尝试格式化的字符串是:

ALTER TABLE "documents_directory" ADD CONSTRAINT "path_endswith_identifier_if_not_root" CHECK (("full_path"::text LIKE '%' || REPLACE(REPLACE(REPLACE((CONCAT('/', CONCAT("documents_directory"."identifier", '/'))), E'\\', E'\\\\'), E'%', E'\\%'), E'_', E'\\_') OR "full_path" = '/'))

元组是空的

我尝试了以下方法来排除简单的愚蠢:

  • 在注释中使用Concat(Value("/"), F("identifier"), Value("/")),这就像一个魅力
  • 替换Value("/")"/",正如预期的那样,这本身会产生问题
  • 用一个简单的值替换约束中的 concat,这也有效

所以看起来所有的拼图都是分开工作的!

如果重要的话,我正在使用 django 2.2.9

4

0 回答 0