Re: [sqlalchemy] Reuse pre-defined Enum?
Hey guys, I am facing the same issue, I am using PostgreSQL and want to use native ENUM type in two tables. For migration I am going to use alembic. Did you guys find out any was to do this? Thanks, Anton On Thursday, May 1, 2014 10:33:21 AM UTC-7, Michael Bayer wrote: OK. ENUM is something I’d have to dedicate several days of attention on :( On May 1, 2014, at 1:32 PM, Vlad Wing vlad...@gmail.com javascript: wrote: Yes, that's exactly what happend. I specified create_type=False and it was ignored. Alembic tried to create the type anyway and, of course, it failed. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com javascript:. To post to this group, send email to sqlal...@googlegroups.com javascript:. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
Re: [sqlalchemy] Reuse pre-defined Enum?
this works fine for me: def upgrade(): mytype = ENUM('a', 'b', 'c', create_type=False, name='myenum') mytype.create(op.get_bind(), checkfirst=False) op.create_table('t1', sa.Column('id', sa.Integer(), nullable=False), sa.Column('col1', mytype) ) op.create_table('t2', sa.Column('id', sa.Integer(), nullable=False), sa.Column('col1', mytype) ) output: INFO [alembic.migration] Running upgrade None - 47bed260c052, initial rev INFO [sqlalchemy.engine.base.Engine] SELECT EXISTS ( SELECT * FROM pg_catalog.pg_type t WHERE t.typname = %(typname)s AND pg_type_is_visible(t.oid) ) INFO [sqlalchemy.engine.base.Engine] {'typname': u'myenum'} INFO [sqlalchemy.engine.base.Engine] CREATE TYPE myenum AS ENUM ('a', 'b', 'c') INFO [sqlalchemy.engine.base.Engine] {} INFO [sqlalchemy.engine.base.Engine] CREATE TABLE t1 ( id INTEGER NOT NULL, col1 myenum ) INFO [sqlalchemy.engine.base.Engine] {} INFO [sqlalchemy.engine.base.Engine] CREATE TABLE t2 ( id INTEGER NOT NULL, col1 myenum ) INFO [sqlalchemy.engine.base.Engine] {} INFO [sqlalchemy.engine.base.Engine] INSERT INTO alembic_version (version_num) VALUES ('47bed260c052') INFO [sqlalchemy.engine.base.Engine] {} INFO [sqlalchemy.engine.base.Engine] COMMIT works with --sql too, output: CREATE TYPE myenum AS ENUM ('a', 'b', 'c'); CREATE TABLE t1 ( id INTEGER NOT NULL, col1 myenum ); CREATE TABLE t2 ( id INTEGER NOT NULL, col1 myenum ); INSERT INTO alembic_version (version_num) VALUES ('47bed260c052'); COMMIT; On 7/3/14, 3:23 PM, Anton wrote: Hey guys, I am facing the same issue, I am using PostgreSQL and want to use native ENUM type in two tables. For migration I am going to use alembic. Did you guys find out any was to do this? Thanks, Anton On Thursday, May 1, 2014 10:33:21 AM UTC-7, Michael Bayer wrote: OK. ENUM is something I’d have to dedicate several days of attention on :( On May 1, 2014, at 1:32 PM, Vlad Wing vlad...@gmail.com javascript: wrote: Yes, that's exactly what happend. I specified create_type=False and it was ignored. Alembic tried to create the type anyway and, of course, it failed. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com javascript:. To post to this group, send email to sqlal...@googlegroups.com javascript:. Visit this group at http://groups.google.com/group/sqlalchemy http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com mailto:sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com mailto:sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
Re: [sqlalchemy] Reuse pre-defined Enum?
use the PG ENUM type instead and add create_type=False: from sqlalchemy.dialects.postgresql import ENUM ENUM(‘male’, ‘female’, name=‘gt’, create_type=False) This method didn't work for me. The safest way I could find is to manually edit the autogenerated script, removing the reference to the column, and then adding the column manually later using op.execute. For example: op.execute(ALTER TABLE ModelB ADD gender gender_type) -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
Re: [sqlalchemy] Reuse pre-defined Enum?
On May 1, 2014, at 1:09 PM, Vlad Wing vlad.w...@gmail.com wrote: use the PG ENUM type instead and add create_type=False: from sqlalchemy.dialects.postgresql import ENUM ENUM('male', 'female', name='gt', create_type=False) This method didn't work for me. The safest way I could find is to manually edit the autogenerated script, removing the reference to the column, and then adding the column manually later using op.execute. For example: op.execute(ALTER TABLE ModelB ADD gender gender_type) OK the pg.ENUM type obviously needs a lot more attention in the future, though just curious what was didn't work in this case, create_type was ignored and it tried to CREATE TYPE anyway ? -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
Re: [sqlalchemy] Reuse pre-defined Enum?
Yes, that's exactly what happend. I specified create_type=False and it was ignored. Alembic tried to create the type anyway and, of course, it failed. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
Re: [sqlalchemy] Reuse pre-defined Enum?
OK. ENUM is something I'd have to dedicate several days of attention on :( On May 1, 2014, at 1:32 PM, Vlad Wing vlad.w...@gmail.com wrote: Yes, that's exactly what happend. I specified create_type=False and it was ignored. Alembic tried to create the type anyway and, of course, it failed. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
Re: [sqlalchemy] Reuse pre-defined Enum?
Hi Michael, On Mon, Dec 16, 2013 at 12:37 AM, Michael Bayer mike...@zzzcomputing.comwrote: On Dec 15, 2013, at 4:16 AM, Hồng Quân Nguyễn ng.hong.q...@gmail.com wrote: Hi all, Assume that I have this model class ModelA: gender = Column(Enum('male', 'female', name='gender_type')) and in another ModelB, I want to reuse this gender_type Enum, how should I reuse, because if I write class ModelB: gender = Column(Enum('male', 'female', name='gender_type')) I will get error about conflict when creating new Enum type”. assuming you’re on Postgresql, where there is a specific type created on the backend. Start like this: gender_type = Enum(‘male’, ‘female’, name=‘gender_type’) then any number of column objects can use it: Column(gender_type) or start with the PG type: from sqlalchemy.dialects.postgresql import ENUM gender_type = ENUM(‘male’, ‘female’, name=‘gender_type’) then additional columns can use the same type, but create_type=False: Column(ENUM(‘male’, ‘female’, name=‘gender_type’, create_type=False)) unfortunately the “create_type” argument is not on the base Enum type right now (looking into adding that). Before, I had gender_type declared inline in model A (gender = Column(Enum('male', 'female', name='gender_type'))). Now I add model B and also want to reuse the gender_type in model A. So, I bring gender_type out and link to it from model A B. But when I use Alembic to create migration script, I got this for model B (new-added): def upgrade(): ... gender = sa.Column('gender', sa.Enum('male', 'female', name='gender_types'), nullable=True) ... It means the migration script tries to create new Enum, instead reuse it. How should I edit the migration script to make it do what I want? Thank you in advance. -- *** * Nguyễn Hồng Quân* * Y!M: ng_hquan_vn* * Facebook: ng.hong.quan * * Web: quan.hoabinh.vn* *** -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
Re: [sqlalchemy] Reuse pre-defined Enum?
On Apr 10, 2014, at 1:12 PM, Nguyễn Hồng Quân ng.hong.q...@gmail.com wrote: Before, I had gender_type declared inline in model A (gender = Column(Enum('male', 'female', name='gender_type'))). Now I add model B and also want to reuse the gender_type in model A. So, I bring gender_type out and link to it from model A B. But when I use Alembic to create migration script, I got this for model B (new-added): def upgrade(): ... gender = sa.Column('gender', sa.Enum('male', 'female', name='gender_types'), nullable=True) ... It means the migration script tries to create new Enum, instead reuse it. How should I edit the migration script to make it do what I want? use the PG ENUM type instead and add create_type=False: from sqlalchemy.dialects.postgresql import ENUM ENUM(‘male’, ‘female’, name=‘gt’, create_type=False) -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
[sqlalchemy] Reuse pre-defined Enum?
Hi all, Assume that I have this model class ModelA: gender = Column(Enum('male', 'female', name='gender_type')) and in another ModelB, I want to reuse this gender_type Enum, how should I reuse, because if I write class ModelB: gender = Column(Enum('male', 'female', name='gender_type')) I will get error about conflict when creating new Enum type. I tried this recipe http://techspot.zzzeek.org/2011/01/14/the-enum-recipe/ but it seems old and doesn't work: I got warning about null IN-predicate, as well as get error 500 when using with Flask-Admin :-( -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out.
Re: [sqlalchemy] Reuse pre-defined Enum?
On Dec 15, 2013, at 4:16 AM, Hồng Quân Nguyễn ng.hong.q...@gmail.com wrote: Hi all, Assume that I have this model class ModelA: gender = Column(Enum('male', 'female', name='gender_type')) and in another ModelB, I want to reuse this gender_type Enum, how should I reuse, because if I write class ModelB: gender = Column(Enum('male', 'female', name='gender_type')) I will get error about conflict when creating new Enum type”. assuming you’re on Postgresql, where there is a specific type created on the backend. Start like this: gender_type = Enum(‘male’, ‘female’, name=‘gender_type’) then any number of column objects can use it: Column(gender_type) or start with the PG type: from sqlalchemy.dialects.postgresql import ENUM gender_type = ENUM(‘male’, ‘female’, name=‘gender_type’) then additional columns can use the same type, but create_type=False: Column(ENUM(‘male’, ‘female’, name=‘gender_type’, create_type=False)) unfortunately the “create_type” argument is not on the base Enum type right now (looking into adding that). I tried this recipe http://techspot.zzzeek.org/2011/01/14/the-enum-recipe/ but it seems old and doesn't work: I got warning about null IN-predicate, as well as get error 500 when using with Flask-Admin :-( -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/groups/opt_out. signature.asc Description: Message signed with OpenPGP using GPGMail