Skip to content

Commit 7520627

Browse files
committed
Fixed array support for SQLAlchemy < 1.1
1 parent 1b877c9 commit 7520627

2 files changed

Lines changed: 25 additions & 2 deletions

File tree

sqlacodegen/codegen.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,18 @@
1111
import sqlalchemy
1212
from sqlalchemy import (
1313
Enum, ForeignKeyConstraint, PrimaryKeyConstraint, CheckConstraint, UniqueConstraint, Table,
14-
Column, ARRAY)
14+
Column)
1515
from sqlalchemy.schema import ForeignKey
1616
from sqlalchemy.sql.expression import TextClause
1717
from sqlalchemy.types import Boolean, String
1818
from sqlalchemy.util import OrderedDict
1919

20+
# The generic ARRAY type was introduced in SQLAlchemy 1.1
21+
try:
22+
from sqlalchemy import ARRAY
23+
except ImportError:
24+
from sqlalchemy.dialects.postgresql import ARRAY
25+
2026
# Conditionally import Geoalchemy2 to enable reflection support
2127
try:
2228
import geoalchemy2 # noqa: F401

tests/test_codegen.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import sys
55
from io import StringIO
66

7+
import sqlalchemy
78
from sqlalchemy.dialects.mysql import base as mysql
89
from sqlalchemy.dialects.mysql.base import TINYINT
910
from sqlalchemy.dialects.postgresql import ARRAY
@@ -93,7 +94,23 @@ def test_arrays(self):
9394
Column('int_array', ARRAY(INTEGER))
9495
)
9596

96-
assert self.generate_code() == """\
97+
if sqlalchemy.__version__ < '1.1':
98+
assert self.generate_code() == """\
99+
# coding: utf-8
100+
from sqlalchemy import Column, Float, Integer, MetaData, Table
101+
from sqlalchemy.dialects.postgresql.base import ARRAY
102+
103+
metadata = MetaData()
104+
105+
106+
t_simple_items = Table(
107+
'simple_items', metadata,
108+
Column('dp_array', ARRAY(Float(precision=53))),
109+
Column('int_array', ARRAY(Integer()))
110+
)
111+
"""
112+
else:
113+
assert self.generate_code() == """\
97114
# coding: utf-8
98115
from sqlalchemy import ARRAY, Column, Float, Integer, MetaData, Table
99116

0 commit comments

Comments
 (0)