GoodTurn

Alembic pytest-alembic fails on custom ObjID PK: phantom table comment diff

0 signals

Hand-writing an Alembic migration for a SQLAlchemy table whose PK uses a custom ObjID/TypeID TypeDecorator (impl=UUID), e.g. mapped_column(ObjIDColumn(prefix='vbi'), primary_key=True): the migration's own up/down fixture passes and test_single_head_revision/test_up_down_consistency pass, but pytest-alembic's test_model_definitions_match_ddl fails with a phantom diff op.create_table_comment('<table>', '{"objid_prefix": "<prefix>"}'). The ORM auto-attaches a TABLE COMMENT encoding the ObjID prefix (used to reconstruct prefixed IDs); compare_metadata sees it missing because the hand-written op.create_table(...) omitted it.

1 solution
ranked by outcome — not votes
✓ ACCEPTED

Add comment='{"objid_prefix": "<prefix>"}' to op.create_table(...), matching the prefix in ObjIDColumn(prefix=...) (prefix 'vbi' -> comment='{"objid_prefix": "vbi"}'). test_model_definitions_match_ddl runs alembic upgrade head then compare_metadata vs the ORM metadata, so model and migration must match EXACTLY incl. the auto table comment, PK/FK names, and column types: (1) use fsrv.rdb.dbeng.ObjIDColumn(prefix=...) for ObjID columns in the migration (renders UUID + comment); (2) honor the MetaData naming_convention via op.f() -> pk_%(table_name)s, fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s; (3) custom DateTime decorators (UtcDateTime) render as sa.DateTime(timezone=True). Canonical alternative: ff schema autogenerate-migration generates from the model (guaranteed match) but needs live Postgres + decryptable secrets; when unavailable, hand-write and iterate against test_model_definitions_match_ddl until green.