[sqlalchemy] Can't create table with ENUM type column

2015-07-05 Thread sector119
Hello,

Help me create table with enum type column, SA do not create enum type 
before table :(
And server_default doesn't set value that I expect, actions 
import_action[] DEFAULT ARRAY['copy'] NOT NULL it set array['copy'], not 
import_action['copy'] as default...

Code and exception:

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column
from sqlalchemy import Integer, UnicodeText
from sqlalchemy.dialects.postgresql import ARRAY, array, ENUM


engine = 
create_engine('postgresql+psycopg2://user:password@127.0.0.1:5432/epsilon', 
echo=True)
Base = declarative_base()
DBSession = sessionmaker(bind=engine)()
Base.metadata.bind = engine

import_action = ENUM('copy', 'insert', 'update', name='import_action', 
metadata=Base.metadata)

class Import(Base):
__tablename__ = 'import'

id = Column(Integer, primary_key=True)
name = Column(UnicodeText, nullable=False)
actions = Column(ARRAY(import_action), server_default=array(['copy'], 
type_=import_action), nullable=False)

Import.__table__.create(checkfirst=True)


sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) type 
import_action[] does not exist
LINE 5:  actions import_action[] DEFAULT ARRAY['copy'] NOT NULL, 
 ^
 [SQL: \nCREATE TABLE import (\n\tid SERIAL NOT NULL, \n\tname TEXT NOT 
NULL, \n\tactions import_action[] DEFAULT ARRAY['copy'] NOT NULL, 
\n\tPRIMARY KEY (id)\n)\n\n]

-- 
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] Can't create table with ENUM type column

2015-07-05 Thread Mike Bayer



On 7/5/15 3:08 PM, sector119 wrote:

Hello,

Help me create table with enum type column, SA do not create enum type 
before table :(


this is issue 
https://bitbucket.org/zzzeek/sqlalchemy/issue/2729/metadatacreate_all-do-not-create-enum-in, 
please see the workaround there.


And server_default doesn't set value that I expect, actions 
import_action[] DEFAULT ARRAY['copy'] NOT NULL it set array['copy'], 
not import_action['copy'] as default...


There is some complexity to render an enum value inside of an ARRAY and 
even the psycopg2 driver has issues with it, see 
https://bitbucket.org/zzzeek/sqlalchemy/issue/3467/array-of-enums-does-not-allow-assigning 
for even more workarounds that you will definitely need (hint: ARRAY of 
ENUM is not worth it).


please use a plain text() construct for your server_default to work 
around any issues simply.  E.g. server_default=text([copy]) or 
whatever you need it to be.





Code and exception:

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column
from sqlalchemy import Integer, UnicodeText
from sqlalchemy.dialects.postgresql import ARRAY, array, ENUM


engine = 
create_engine('postgresql+psycopg2://user:password@127.0.0.1:5432/epsilon', 
echo=True)

Base = declarative_base()
DBSession = sessionmaker(bind=engine)()
Base.metadata.bind = engine

import_action = ENUM('copy', 'insert', 'update', name='import_action', 
metadata=Base.metadata)


class Import(Base):
__tablename__ = 'import'

id = Column(Integer, primary_key=True)
name = Column(UnicodeText, nullable=False)
actions = Column(ARRAY(import_action), 
server_default=array(['copy'], type_=import_action), nullable=False)


Import.__table__.create(checkfirst=True)


sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) type 
import_action[] does not exist

LINE 5:  actions import_action[] DEFAULT ARRAY['copy'] NOT NULL,
 ^
 [SQL: \nCREATE TABLE import (\n\tid SERIAL NOT NULL, \n\tname TEXT 
NOT NULL, \n\tactions import_action[] DEFAULT ARRAY['copy'] NOT NULL, 
\n\tPRIMARY KEY (id)\n)\n\n]

--
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] Can't create table with ENUM type column

2015-07-05 Thread sector119
Thanks a lot for your help, Michael !

-- 
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.