python - Alembic:包括约束以拒绝带有字符的字符串

标签 python sqlalchemy sql-like alembic

我有一个看起来像这样的表

> select * from mytable
  id        value
0  1  hello world
1  2  hello_world
2  3  hello+world

我试图施加一个 alembic 检查约束,其中 value 中的值不能有 : 字符。我将如何使用 alembic.op 对象执行此操作? upgrade()downgrade() 函数是什么?

编辑:我使用的数据库是 Mysql。

最佳答案

在这种情况下,检查约束起作用

def upgrade():
    op.create_check_constraint("constraint_name_here", "mytable", "value not like '%:%'")

def downgrade():
    op.drop_constraint("constraint_name_here", "mytable", type_="check")

但是mysql不支持检查约束。 ¯_(ツ)_/¯

关于python - Alembic:包括约束以拒绝带有字符的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40540222/

相关文章:

python - 设置 Emacs 以使用 elpy 运行 python

python - 使用 Python 将 HTML 表示形式替换为 ascii

python - 将文本与 python 中的多个正则表达式匹配

python、pandas、sqlalchemy - 在查询中转义的符号

mysql - match() against() 不起作用,但像 '%$s%' 一样

mysql - 如何在 MySQL 中取消十六进制并进行不区分大小写的搜索?

python - 在 Python 中,如何用另一个列表索引一个列表?

python - Flask-SQLAlchemy - 按关注者数量显示顺序

python - SQLAlchemy:多对多关系查询中的过滤计数

SQL 参数与白名单列值比较