def _get_column_info(self, name, format_type, default,
notnull, domains, enums, schema, comment):
+ def _handle_array_type(attype):
+ return (
+ attype.replace('[]', ''), # strip '[]' from integer[], etc.
+ attype.endswith('[]'),
+ )
+
# strip (*) from character varying(5), timestamp(5)
# with time zone, geometry(POLYGON), etc.
attype = re.sub(r'\(.*\)', '', format_type)
# strip quotes from case sensitive enum names
attype = re.sub(r'^"|"$', '', attype)
- # strip '[]' from integer[], etc.
- attype = attype.replace('[]', '')
+ # strip '[]' from integer[], etc. and check if an array
+ attype, is_array = _handle_array_type(attype)
nullable = not notnull
- is_array = format_type.endswith('[]')
+
charlen = re.search(r'\(([\d,]+)\)', format_type)
if charlen:
charlen = charlen.group(1)
elif attype in domains:
domain = domains[attype]
attype = domain['attype']
+ attype, is_array = _handle_array_type(attype)
# A table can't override whether the domain is nullable.
nullable = domain['nullable']
if domain['default'] and not default:
from sqlalchemy import exc
import sqlalchemy as sa
from sqlalchemy.dialects.postgresql import base as postgresql
-from sqlalchemy.dialects.postgresql import ARRAY, INTERVAL, TSRANGE
+from sqlalchemy.dialects.postgresql import ARRAY, INTERVAL, INTEGER, TSRANGE
from sqlalchemy.dialects.postgresql import ExcludeConstraint
import re
'CREATE DOMAIN testdomain INTEGER NOT NULL DEFAULT 42', \
'CREATE DOMAIN test_schema.testdomain INTEGER DEFAULT 0', \
"CREATE TYPE testtype AS ENUM ('test')", \
- 'CREATE DOMAIN enumdomain AS testtype':
+ 'CREATE DOMAIN enumdomain AS testtype', \
+ 'CREATE DOMAIN arraydomain AS INTEGER[]':
try:
con.execute(ddl)
except exc.DBAPIError as e:
con.execute('CREATE TABLE enum_test (id integer, data enumdomain)')
+ con.execute('CREATE TABLE array_test (id integer, data arraydomain)')
+
@classmethod
def teardown_class(cls):
con = testing.db.connect()
con.execute("DROP TABLE enum_test")
con.execute("DROP DOMAIN enumdomain")
con.execute("DROP TYPE testtype")
+ con.execute('DROP TABLE array_test')
+ con.execute('DROP DOMAIN arraydomain')
def test_table_is_reflected(self):
metadata = MetaData(testing.db)
['test']
)
+ def test_array_domain_is_reflected(self):
+ metadata = MetaData(testing.db)
+ table = Table('array_test', metadata, autoload=True)
+ eq_(
+ table.c.data.type.__class__,
+ ARRAY
+ )
+ eq_(
+ table.c.data.type.item_type.__class__,
+ INTEGER
+ )
+
def test_table_is_reflected_test_schema(self):
metadata = MetaData(testing.db)
table = Table('testtable', metadata, autoload=True,