用sqlalchemy创建的Snowflake table需要引号("")查询
Snowflake table created with sqlalchemy requires quotes ("") to query
我正在使用 python 和 sqlalchemy 将数据摄取到雪花 tables 中。我创建的这些 table 都需要引号来查询 table 名称和列名称。例如: select * from "database"."schema"."table" where "column" = 2;
会运行,而select * from database.schema.table where column = 2;
不会运行。区别在于引号。
我知道如果 table 在 Snowflake 中创建时带有引号,则查询它时需要引号。但是,我只将 excel 文件放入 pandas 数据框中,然后使用 sqlalchemy 和 pd.to_sql 创建 table。我的代码示例:
engine = create_engine(URL(
account = 'my_account',
user = 'my_username',
password = 'my_password',
database = 'My_Database',
schema = 'My_Schema',
warehouse = 'My_Wh',
role='My Role',
))
connection = engine.connect()
df.to_sql('My_Table', con=engine, if_exists='replace', index=False, index_label=None, chunksize=16384)
sqlalchemy 会自动创建带引号的 table 吗?这是模式的问题吗?我没有设置它。有解决办法吗?
感谢您的帮助,不胜感激!
来自 SQLAlchemy 雪花 Github documentation:
Object Name Case Handling
Snowflake stores all case-insensitive object
names in uppercase text. In contrast, SQLAlchemy considers all
lowercase object names to be case-insensitive. Snowflake SQLAlchemy
converts the object name case during schema-level communication, i.e.
during table and index reflection. If you use uppercase object names,
SQLAlchemy assumes they are case-sensitive and encloses the names with
quotes. This behavior will cause mismatches agaisnt data dictionary
data received from Snowflake, so unless identifier names have been
truly created as case sensitive using quotes, e.g., "TestDb", all
lowercase names should be used on the SQLAlchemy side.
我认为这是想说的是,SQLAlchemy 将任何包含大写字母的名称视为区分大小写,并自动将它们括在引号中,相反,任何小写字母的名称都不会被引用。看起来这种行为是不可配置的。
您可能无法控制数据库和可能的架构名称,但是在创建 table 时,如果您想要一致的行为,无论是引用还是未引用,那么您应该坚持使用小写命名。您应该发现,无论您使用 "my_table"
还是 my_table
.
,table 名称都可以使用
我正在使用 python 和 sqlalchemy 将数据摄取到雪花 tables 中。我创建的这些 table 都需要引号来查询 table 名称和列名称。例如: select * from "database"."schema"."table" where "column" = 2;
会运行,而select * from database.schema.table where column = 2;
不会运行。区别在于引号。
我知道如果 table 在 Snowflake 中创建时带有引号,则查询它时需要引号。但是,我只将 excel 文件放入 pandas 数据框中,然后使用 sqlalchemy 和 pd.to_sql 创建 table。我的代码示例:
engine = create_engine(URL(
account = 'my_account',
user = 'my_username',
password = 'my_password',
database = 'My_Database',
schema = 'My_Schema',
warehouse = 'My_Wh',
role='My Role',
))
connection = engine.connect()
df.to_sql('My_Table', con=engine, if_exists='replace', index=False, index_label=None, chunksize=16384)
sqlalchemy 会自动创建带引号的 table 吗?这是模式的问题吗?我没有设置它。有解决办法吗?
感谢您的帮助,不胜感激!
来自 SQLAlchemy 雪花 Github documentation:
Object Name Case Handling
Snowflake stores all case-insensitive object names in uppercase text. In contrast, SQLAlchemy considers all lowercase object names to be case-insensitive. Snowflake SQLAlchemy converts the object name case during schema-level communication, i.e. during table and index reflection. If you use uppercase object names, SQLAlchemy assumes they are case-sensitive and encloses the names with quotes. This behavior will cause mismatches agaisnt data dictionary data received from Snowflake, so unless identifier names have been truly created as case sensitive using quotes, e.g., "TestDb", all lowercase names should be used on the SQLAlchemy side.
我认为这是想说的是,SQLAlchemy 将任何包含大写字母的名称视为区分大小写,并自动将它们括在引号中,相反,任何小写字母的名称都不会被引用。看起来这种行为是不可配置的。
您可能无法控制数据库和可能的架构名称,但是在创建 table 时,如果您想要一致的行为,无论是引用还是未引用,那么您应该坚持使用小写命名。您应该发现,无论您使用 "my_table"
还是 my_table
.