The same ENUM during migration

1,454 views
Skip to first unread message

Victor Varvariuc

unread,
Jul 9, 2013, 7:52:12 AM7/9/13
to sqlalchem...@googlegroups.com
I have this model:

class Subscription(Base):
    __tablename__ = 'subcsription'
    gender = Column(GENDER_CHOICES)  # Gender
    auto_gender = Column(GENDER_CHOICES)  # Gender

And i get during migration:

sqlalchemy.exc.ProgrammingError: (ProgrammingError) type "gender_types" already exists
 "CREATE TYPE gender_types AS ENUM ('M','F')" {}

How to fix this? Thanks!

Michael Bayer

unread,
Jul 9, 2013, 10:28:14 AM7/9/13
to sqlalchem...@googlegroups.com
you might need to disable the creation, use postgresql.ENUM as the base class directly, then:

GENDER_CHOICES(create_type=False)

then at any time you can create the type if needed:

GENDER_CHOICES().create(op.bind)


if you are using just types.Enum, it might be easier to just use CHAR enums across the board, setting up GENDER_CHOICES like:

GENDER_CHOICES(native_enum=False)






--
You received this message because you are subscribed to the Google Groups "sqlalchemy-alembic" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy-alem...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Victor Varvariuc

unread,
Jul 10, 2013, 2:14:09 AM7/10/13
to sqlalchem...@googlegroups.com
If I do:

GENDER_CHOICES = Enum('M', 'F', name='gender_types', native_enum=False)

Alembic generates:

    sa.Column('gender', sa.Enum('M', 'F', native_enum=False, name='gender_types'), nullable=True),
    sa.Column('auto_gender', sa.Enum('M', 'F', native_enum=False, name='gender_types'), nullable=True),

And the migration fails:

sqlalchemy.exc.ProgrammingError: (ProgrammingError) check constraint "gender_types" already exists
 "\nCREATE TABLE subcsription (\n\tid SERIAL NOT NULL, \n\tcustomer_id INTEGER, \n\temail VARCHAR(150) NOT NULL, \n\tstatus status_choices NOT NULL, \n\tsms_status sms_status_choices NOT NULL, \n\tstatus_changed_at TIMESTAMP WITHOUT TIME ZONE, \n\tconfirm_code VARCHAR(50) NOT NULL, \n\tgender VARCHAR(1), \n\tauto_gender VARCHAR(1), \n\tlocation VARCHAR(255), \n\tutm_source VARCHAR(255), \n\tutm_medium VARCHAR(255), \n\tutm_campaign VARCHAR(255), \n\tutm_content VARCHAR(255), \n\tutm_term TEXT, \n\tutm_keyword VARCHAR(255), \n\turl TEXT, \n\treferer TEXT, \n\tdomain domain_choices, \n\tname VARCHAR(255), \n\tyear_of_birth INTEGER, \n\tregion VARCHAR(64), \n\tcity VARCHAR(150), \n\tcreated_at TIMESTAMP WITHOUT TIME ZONE DEFAULT 'now()' NOT NULL, \n\tupdated_at TIMESTAMP WITHOUT TIME ZONE DEFAULT 'now()' NOT NULL, \n\tPRIMARY KEY (id), \n\tFOREIGN KEY(customer_id) REFERENCES customer (id) ON DELETE CASCADE, \n\tCONSTRAINT gender_types CHECK (gender IN ('M', 'F')), \n\tCONSTRAINT gender_types CHECK (auto_gender IN ('M', 'F'))\n)\n\n" {}


Michael Bayer

unread,
Jul 10, 2013, 10:41:17 AM7/10/13
to sqlalchem...@googlegroups.com
you'd need to adjust the autogeneration to display GENDER_CHOICES.    If you'd like to automate this, you can provide a special renderer to the environment, see the "render_item" argument to EnvironmentContext.configure: https://alembic.readthedocs.org/en/latest/api.html#alembic.environment.EnvironmentContext.configure




Reply all
Reply to author
Forward
0 new messages