sqlalchemy - Alembic:如何向现有列添加唯一约束

标签 sqlalchemy flask-sqlalchemy alembic

我有一个表 'test' 有一个没有约束的列 'Name'。我需要ALTER给它一个 UNIQUE约束。我该怎么做?

我应该使用 op.alter_column('???')create_unique_constraint('???') ?
create_unique_constraint 不是用于新列而不是现有列吗?

最佳答案

要添加,您需要:
https://alembic.sqlalchemy.org/en/latest/ops.html#alembic.operations.Operations.create_unique_constraint

from alembic import op
op.create_unique_constraint('uq_user_name', 'user', ['name'], schema='my_schema')

要放弃,您需要:
https://alembic.sqlalchemy.org/en/latest/ops.html#alembic.operations.Operations.drop_constraint
op.drop_constraint('uq_user_name', 'user', schema='my_schema')

关于sqlalchemy - Alembic:如何向现有列添加唯一约束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16589315/

相关文章:

python - SQLAlchemy如何获取字段的最大值?

python - Alembic --autogenerate 尝试重新创建每个表

python - 应该如何使用 Alembic 删除 SQLAlchemy-Searchable 触发器

python - 使用 Flask SQLAlchemy SELECT DISTINCT YEAR()

Python Flask SQLalchemy 外键未保存在数据库中 - 一对多

postgresql - Alembic 未生成正确的更改

mysql - Flask Sqlalchemy 如何在更新行时不自动更新 TIMESTAMP 列

python - 在迁移中保留数据

python - SqlAlchemy:如何在用户之间创建连接,即 make "friends"

flask - Flask-SQLAlchemy 中的一对一关系仅以一种方式工作