我试图反映一个表,同时使用
include_columns
。但是,忽略具有ForeignKeyConstraint的列会导致异常:
Traceback (most recent call last):
File "/usr/local/lib/python3.9/dist-packages/sqlalchemy/sql/schema.py", line 3825, in _set_parent
ColumnCollectionConstraint._set_parent(self, table)
File "/usr/local/lib/python3.9/dist-packages/sqlalchemy/sql/schema.py", line 3414, in _set_parent
ColumnCollectionMixin._set_parent(self, table)
File "/usr/local/lib/python3.9/dist-packages/sqlalchemy/sql/schema.py", line 3371, in _set_parent
for col in self._col_expressions(table):
File "/usr/local/lib/python3.9/dist-packages/sqlalchemy/sql/schema.py", line 3365, in _col_expressions
return [
File "/usr/local/lib/python3.9/dist-packages/sqlalchemy/sql/schema.py", line 3366, in <listcomp>
table.c[col] if isinstance(col, util.string_types) else col
File "/usr/local/lib/python3.9/dist-packages/sqlalchemy/sql/base.py", line 1213, in __getitem__
return self._index[key]
KeyError: 'DiscountDefinition_FK'
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File ".../script.py", line 62, in <module>
POSBillItem = Table('POSBillItem', metadata, include_columns=POSBillItem_columns, autoload_with=engine)
File "<string>", line 2, in __new__
File "/usr/local/lib/python3.9/dist-packages/sqlalchemy/util/deprecations.py", line 309, in warned
return fn(*args, **kwargs)
File "/usr/local/lib/python3.9/dist-packages/sqlalchemy/sql/schema.py", line 607, in __new__
metadata._remove_table(name, schema)
File "/usr/local/lib/python3.9/dist-packages/sqlalchemy/util/langhelpers.py", line 70, in __exit__
compat.raise_(
File "/usr/local/lib/python3.9/dist-packages/sqlalchemy/util/compat.py", line 207, in raise_
raise exception
File "/usr/local/lib/python3.9/dist-packages/sqlalchemy/sql/schema.py", line 602, in __new__
table._init(name, metadata, *args, **kw)
File "/usr/local/lib/python3.9/dist-packages/sqlalchemy/sql/schema.py", line 677, in _init
self._autoload(
File "/usr/local/lib/python3.9/dist-packages/sqlalchemy/sql/schema.py", line 712, in _autoload
conn_insp.reflect_table(
File "/usr/local/lib/python3.9/dist-packages/sqlalchemy/engine/reflection.py", line 795, in reflect_table
self._reflect_fk(
File "/usr/local/lib/python3.9/dist-packages/sqlalchemy/engine/reflection.py", line 997, in _reflect_fk
table.append_constraint(
File "/usr/local/lib/python3.9/dist-packages/sqlalchemy/sql/schema.py", line 913, in append_constraint
constraint._set_parent_with_dispatch(self)
File "/usr/local/lib/python3.9/dist-packages/sqlalchemy/sql/base.py", line 1046, in _set_parent_with_dispatch
self._set_parent(parent, **kw)
File "/usr/local/lib/python3.9/dist-packages/sqlalchemy/sql/schema.py", line 3827, in _set_parent
util.raise_(
File "/usr/local/lib/python3.9/dist-packages/sqlalchemy/util/compat.py", line 207, in raise_
raise exception
sqlalchemy.exc.ArgumentError: Can't create ForeignKeyConstraint on table 'POSBillItem': no column named 'DiscountDefinition_FK' is present.
from sqlalchemy import create_engine, engine
from sqlalchemy import MetaData, Table
conn_string = engine.URL.create(
"mssql+pyodbc",
username,
password,
host,
port,
db,
query={"driver": "ODBC Driver 18 for SQL Server", "TrustServerCertificate": "YES"}
)
engine = create_engine(conn_string)
metadata = MetaData()
Table1_columns = [
"PosBill_FK",
"Quantity",
"UnitPrice",
#"DiscountDefinition_FK",
#"originID"
]
POSBillItem = Table('POSBillItem', metadata, include_columns=Table1_columns, autoload_with=engine)
如果我取消注释
"DiscountDefinition_FK"
,异常不再发生,但我收到一条警告:
.../script.py:62: SAWarning: Omitting index key for (originID), key covers omitted columns.
POSBillItem = Table('POSBillItem', metadata, include_columns=POSBillItem_columns, autoload_with=engine)
如果我取消注释
"originID"
,警告也消失了。
有没有办法避免在不手动添加列的情况下加载这些列?