迁移尝试向数据库添加约束失败

时间:2019-08-10 15:04:01

标签: python django django-models

我正在尝试使用django模型在数据库上创建约束,我在这里做错了什么?

我在python 3.7.3上使用django 2.2.2

这是我的模特班:

   class Client(models.Model):
    class Meta:
        db_table = 'Client'
        constraints = [
            models.CheckConstraint(check=models.Q(cnpj__regex='\\d{14}'), name='valid_cnpj'),
        ]

    id = HashidAutoField(primary_key=True)
    trade_name = models.CharField(max_length=200)
    cnpj = models.CharField(max_length=14)
    business_name = models.CharField(max_length=200, null=True)

错误消息:AttributeError:'str'对象没有属性'connector'

完整追溯:

Running migrations:
  Applying global_models.0008_auto_20190809_1747...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 "C:\Users\Celere.LAPTOP-FLPOSE6C\PycharmProjects\api_django\venv\lib\site-packages\django\core\management\__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "C:\Users\Celere.LAPTOP-FLPOSE6C\PycharmProjects\api_django\venv\lib\site-packages\django\core\management\__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\Celere.LAPTOP-FLPOSE6C\PycharmProjects\api_django\venv\lib\site-packages\django\core\management\base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\Celere.LAPTOP-FLPOSE6C\PycharmProjects\api_django\venv\lib\site-packages\django\core\management\base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "C:\Users\Celere.LAPTOP-FLPOSE6C\PycharmProjects\api_django\venv\lib\site-packages\django\core\management\base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "C:\Users\Celere.LAPTOP-FLPOSE6C\PycharmProjects\api_django\venv\lib\site-packages\django\core\management\commands\migrate.py", line 234, in handle
    fake_initial=fake_initial,
  File "C:\Users\Celere.LAPTOP-FLPOSE6C\PycharmProjects\api_django\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 "C:\Users\Celere.LAPTOP-FLPOSE6C\PycharmProjects\api_django\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 "C:\Users\Celere.LAPTOP-FLPOSE6C\PycharmProjects\api_django\venv\lib\site-packages\django\db\migrations\executor.py", line 245, in apply_migration
    state = migration.apply(state, schema_editor)
  File "C:\Users\Celere.LAPTOP-FLPOSE6C\PycharmProjects\api_django\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 "C:\Users\Celere.LAPTOP-FLPOSE6C\PycharmProjects\api_django\venv\lib\site-packages\django\db\migrations\operations\models.py", line 827, in database_forwards
    schema_editor.add_constraint(model, self.constraint)
  File "C:\Users\Celere.LAPTOP-FLPOSE6C\PycharmProjects\api_django\venv\lib\site-packages\django\db\backends\base\schema.py", line 343, in add_constraint
    sql = constraint.create_sql(model, self)
  File "C:\Users\Celere.LAPTOP-FLPOSE6C\PycharmProjects\api_django\venv\lib\site-packages\django\db\models\constraints.py", line 47, in create_sql
    check = self._get_check_sql(model, schema_editor)
  File "C:\Users\Celere.LAPTOP-FLPOSE6C\PycharmProjects\api_django\venv\lib\site-packages\django\db\models\constraints.py", line 37, in _get_check_sql
    where = query.build_where(self.check)
  File "C:\Users\Celere.LAPTOP-FLPOSE6C\PycharmProjects\api_django\venv\lib\site-packages\django\db\models\sql\query.py", line 1296, in build_where
    return self._add_q(q_object, used_aliases=set(), allow_joins=False, simple_col=True)[0]
  File "C:\Users\Celere.LAPTOP-FLPOSE6C\PycharmProjects\api_django\venv\lib\site-packages\django\db\models\sql\query.py", line 1302, in _add_q
    connector = q_object.connector

0 个答案:

没有答案