我正在使用熊猫将数据集动态插入Redshift数据库中。
但是当我尝试更改VARCHAR字段的长度时,它似乎不起作用。
import pandas as pd
import sqlalchemy
engine = sqlalchemy.create_engine("postgresql://credentials")
df = pd.DataFrame([[1,2,3,4,5,6,7], ['a','b','a','c','a','a','a']])
df.to_sql('test_table',con=engine,
if_exists = 'append',
schema='test_schema',
index = False,
dtype={'0': sqlalchemy.types.VARCHAR(length=500)})
在psql中,当我要求\d test
时,它返回:
Table "test_schema.test_table"
Column | Type | Modifiers
-------+------------------------+-----------
0 | character varying(256) |
1 | character varying(256) |
2 | character varying(256) |
3 | character varying(256) |
4 | character varying(256) |
5 | character varying(256) |
6 | character varying(256) |
0
列仍仅接受256个字符。