我正在尝试在数据库中构建一个表集合,这些表都由单个模式拥有。这是我的问题的简化示例。
from sqlalchemy.schema import CreateSchema
import sqlalchemy as sqla
import pandas as pd
# I have the database 'db' already created and owned by 'user'
db_url = 'postgresql://user:password@host:5432/db'
# Let's connect to it via sqla
conn = sqla.create_engine(db_url)
# My db is currently empty, let's first create a schema and then populate it
conn.execute(CreateSchema('my_schema'))
# Verify the schema has been added to the db
insp = sqla.inspect(conn)
schema_list = insp.get_schema_names() #checking this variable, it has indeed
# I create a dataframe
df = pd.DataFrame({'foo': [1,2], 'bar':[3,4]})
现在是令人困惑的部分。如果我这样做:
df.to_sql('my_table', db_url)
然后它工作正常,我得到:
db=> \dt
List of relations
Schema | Name | Type | Owner
--------+------+-------+-------------
public | my_table | table | user
(1 row)
但是,如果我将架构添加为参数df.to_sql('my_table', db_url, schema='my_schema')
,则表格未上传我
db=> \dt
No relations found.
为什么在指定架构时pd.df.to_sql无效?
db=> \dt my_schema.*
List of relations
Schema | Name | Type | Owner
--------+------+-------+-------------
my_schema | my_table | table | user
这表明该表确实正在创建并附加到预期的模式。但是,它仍然是db的一部分吗?为什么它不会出现在\ dt ??
下