if you have an Enum with a certain name that is to be used in multiple tables, you can create it separately. Build the ENUM using the create_type=False flag to keep it from generating the type automatically, then use the .create() method to create it individually, docs at http://docs.sqlalchemy.org/en/rel_1_0/dialects/postgresql.html?highlight=enum#sqlalchemy.dialects.postgresql.ENUM.

Alternatively, if you don't mind the extra overhead, run all your table.create() calls with checkfirst=True. This should run a "checkfirst" query not just for the table itself but also for the ENUM types contained within.




On 08/22/2016 04:03 PM, Fran Goitia wrote:
Models FacebookPost and TwitterPost share an enum called types. This
enum is correctly created when creating facebook_posts table, but when
trying to create twitter_posts table, there is an attempt to recreate
this type which results in an error.


|
sqlalchemy.exc.ProgrammingError:(psycopg2.ProgrammingError)type
"types"already exists
     [SQL:"CREATE TYPE types AS ENUM ('Video', 'GIF', 'Scratch Reel',
'Card', 'Video Card', 'Text', 'Photo', 'Shared Article', 'Reply',
'Canvas', 'Carousel', 'Video Carousel', 'Link', 'Status')"]
|


This is the way I'm creating the database. I can't use
Base.metadata.create_all, because I need to be explicit in terms of what
tables are created


|
Engine=create_engine(db_url,echo=False)
    Campaign.__table__.create(Engine)
    SubCampaign.__table__.create(Engine)
    Creative.__table__.create(Engine)
    Hashtag.__table__.create(Engine)
    FacebookPost.__table__.create(Engine)
    TwitterPost.__table__.create(Engine)

|


I'm creating the enums this way:


|
fromsqlalchemy importEnum
    types =('Video','GIF','Scratch Reel','Card','Video Card',
             'Text','Photo','Shared Article','Reply','Canvas',
             'Carousel','Video Carousel','Link','Status')
    goals =('CTR','ER','Awareness','CPGA')
    sources =('Facebook','Twitter','Instagram','Tumblr')

    vars_ =locals().copy()
    fork,v invars_.items():
        ifisinstance(v,tuple):
            locals()[k]=Enum(*v,name=k)
|

Thanks

--
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 https://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 https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to