sqlalchemy / sqlalchemy/alembic

Unexpected warning for computed column when migrating with MariaDB

Open
#1,607 5 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

autogenerate - detection mysql
Dominant language
Python
Stars
4.4k
Forks
375
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at alembic/autogenerate/compare.py around line 1029 and reproduce the second flask db migrate run with the MariaDB computed column example. Compare the metadata and connection defaults shown in the report; done means the unchanged schema generates no warning and still reports no schema changes.

Written by the indexing model from the issue text.

Assessment

Tech stack
python, sqlalchemy
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.