sqlalchemy / sqlalchemy/alembic

detect check constraint changes applied to schema types esp non native ENUM

Open
#363 30 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Migrated issue, originally created by d9k

Hi!
I use alembic 0.8.5 (tried at 0.8.0 at first, then updated), postgresql 9.3, ubuntu 14.04

I have migration code

op.add_column('my_table', sa.Column('status',
  sa.Enum('one', 'two', 'three', name='test_enum', native_enum=False), nullable=True
))

after migration apply and database dump I see

CREATE TABLE my_table (
-- . . . . .
    status character varying(5),
    CONSTRAINT test_enum CHECK (((status)::text = ANY ((ARRAY['one'::character varying, 'two'::character varying, 'three'::character varying])::text[])))
);

After this I try to alter enum typed field with this new autogenerated from model migration:

op.alter_column('my_table', 'status',
   existing_type=sa.VARCHAR(length=5),
   type_=sa.Enum('one', 'two', 'three', 'four', name='test_enum', native_enum=False),
   existing_nullable=True)

And I get error on migration apply:

sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) constraint "test_enum" for relation "my_table" already exists

I suggest that old constraint (if exists) must be removed prior to new constraint creation. It must be considered that old type for non-native enum is VARCHAR. As in my previous issue https://bitbucket.org/zzzeek/alembic/issues/362/postgresql-migration-float-precision-fail it looks like alembic developers didn't make alembic autogeneration script aware of alembic's own hacks for different RDBMS'es which it uses to alter/create a schema. Why?

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

Reproduce the reported op.alter_column migration on PostgreSQL with a non-native Enum and compare the generated schema before and after adding “four”. Trace Alembic autogeneration and PostgreSQL handling for the VARCHAR-backed enum constraint; done means the migration detects the existing check constraint and applies the changed enum without a duplicate-constraint error.

Written by the indexing model from the issue text.

Assessment

Tech stack
postgresql, python
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.