Unexpected warning for computed column when migrating with MariaDB

未关闭
#1,607 5 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
45/100
Issue 类型
缺陷
描述清晰度
基本清楚
活跃度
停滞
技术栈
python, sqlalchemy
领域
databases

调研方向

从 alembic/autogenerate/compare.py 第 1029 行附近开始,使用 MariaDB 计算列示例重现第二次运行 flask db migrate 的过程。比较报告中显示的元数据和连接默认值;完成的标准是,未更改的 schema 不生成警告,并且仍然报告没有 schema 更改。

由索引模型根据 Issue 内容生成。

描述

autogenerate - detection mysql

Describe the bug

Warning is emitted when generating migration for model with computed column.

Expected behavior

No warning emitted.

To Reproduce

Code

# app.py

import os

import flask
import flask_sqlalchemy as fsa
import flask_migrate as fm
import sqlalchemy as sa


app = flask.Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = (
    f'mariadb+pymysql://{os.environ['DB_USER']}:{os.environ['DB_PASSWORD']}'
    '@localhost:{os.environ['DB_PORT']}/{os.environ['DB_NAME']}'
)

db = fsa.SQLAlchemy(app)
migrate = fm.Migrate(app, db)

class RecordWithComputed(db.Model):
    id = sa.Column(sa.Integer, primary_key=True)
    created_at = sa.Column(sa.DateTime, nullable=False)
    deleted_at = sa.Column(sa.DateTime)

    is_active_record = sa.Column(sa.Boolean, sa.Computed('CASE WHEN deleted_at IS NULL THEN TRUE ELSE NULL END'))

Steps to reproduce

  1. Create and apply first migration.
  2. Try creating second migration.

Error

Output for first migration

flask db migrate -m "first migration"
INFO  [alembic.runtime.migration] Context impl MariaDBImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.autogenerate.compare] Detected added table 'record_with_computed'
  Generating /Users/eganma/junk/computed-column-warning/migrations/versions/15d57df5db98_first_migration.py ...  done

Output for second migration (no changes made to app.py)

flask db migrate -m "second migration"
INFO  [alembic.runtime.migration] Context impl MariaDBImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
/Users/eganma/Library/Caches/pypoetry/virtualenvs/computed-column-warning-swI5pdsd-py3.12/lib/python3.12/site-packages/alembic/autogenerate/compare.py:1034: UserWarning: Computed default on record_with_computed.is_active_record cannot be modified
  util.warn("Computed default on %s.%s cannot be modified" % (tname, cname))
INFO  [alembic.env] No changes in schema detected.

Strings being compared at compare.py:1029

rendered_metadata_default = 'casewhendeleted_atisnullthentrueelsenullend'
rendered_conn_default = 'casewhendeleted_atisnullthen1elsenullend'

Versions.

  • OS: macOS
  • Python: 3.12.0
  • Alembic: 1.14.1
  • SQLAlchemy: 2.0.38
  • Database: MariaDB 10.11.6
  • DBAPI: pymysql 1.1.1

Additional context

Doesn't look like this is causing problems for me at this time, but previous reports I found (#1151, #1462) were focused on postgres. Issue seems to stem from TRUE being reported as the boolean equivalent 1 when comparing the model against the schema.

主要语言
Python
星标
4.5k
派生
391
PR 合并指标
30 天内没有已合并 PR

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

sqlalchemy/alembic 的其他 Issue

查看 sqlalchemy/alembic 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。