Skip to content

Commit 32a2c6f

Browse files
Merge pull request #9138 from jorisvandenbossche/sql-9083-dtype-no-subclass
FIX: to_sql dtype argument accepting SQLAlchemy type instance (GH9083)
2 parents fa83859 + aad5ea5 commit 32a2c6f

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

doc/source/whatsnew/v0.16.0.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ Bug Fixes
6464

6565
- Fixed bug in ``to_sql`` when mapping a Timestamp object column (datetime
6666
column with timezone info) to the according sqlalchemy type (:issue:`9085`).
67-
67+
- Fixed bug in ``to_sql`` ``dtype`` argument not accepting an instantiated
68+
SQLAlchemy type (:issue:`9083`).
6869

6970

7071

pandas/io/sql.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1159,9 +1159,9 @@ def to_sql(self, frame, name, if_exists='fail', index=True,
11591159
11601160
"""
11611161
if dtype is not None:
1162-
import sqlalchemy.sql.type_api as type_api
1162+
from sqlalchemy.types import to_instance, TypeEngine
11631163
for col, my_type in dtype.items():
1164-
if not issubclass(my_type, type_api.TypeEngine):
1164+
if not isinstance(to_instance(my_type), TypeEngine):
11651165
raise ValueError('The type of %s is not a SQLAlchemy '
11661166
'type ' % col)
11671167

pandas/io/tests/test_sql.py

+8
Original file line numberDiff line numberDiff line change
@@ -1229,6 +1229,14 @@ def test_dtype(self):
12291229
self.assertRaises(ValueError, df.to_sql,
12301230
'error', self.conn, dtype={'B': str})
12311231

1232+
# GH9083
1233+
df.to_sql('dtype_test3', self.conn, dtype={'B': sqlalchemy.String(10)})
1234+
meta.reflect()
1235+
sqltype = meta.tables['dtype_test3'].columns['B'].type
1236+
print(sqltype)
1237+
self.assertTrue(isinstance(sqltype, sqlalchemy.String))
1238+
self.assertEqual(sqltype.length, 10)
1239+
12321240
def test_notnull_dtype(self):
12331241
cols = {'Bool': Series([True,None]),
12341242
'Date': Series([datetime(2012, 5, 1), None]),

0 commit comments

Comments
 (0)