我正在尝试使用SQLAlchemy automap扩展为现有数据库生成ORM,并且收到InvalidRequestError异常(“实例无法刷新 - 它不是持久性的,并且不包含完整的主键 . ”)每当我尝试插入到使用由时间戳和外键组成的复合主键的表中 .
这是一些重现问题的最小示例代码:
from sqlalchemy import create_engine, func, select
from sqlalchemy.orm import sessionmaker
from sqlalchemy.sql.expression import text
from sqlalchemy.ext.automap import automap_base
db_schema_cmds = [
'''CREATE TABLE users
(
u_id INTEGER NOT NULL,
name TEXT NOT NULL,
CONSTRAINT Key1 PRIMARY KEY (u_id)
);''',
'''CREATE TABLE posts
(
timestamp TEXT NOT NULL,
text TEXT NOT NULL,
u_id INTEGER NOT NULL,
CONSTRAINT Key2 PRIMARY KEY (timestamp,u_id),
CONSTRAINT users_have_posts FOREIGN KEY (u_id) REFERENCES users (u_id) ON DELETE CASCADE
);''']
# Create a new in-memory SQLite DB and execute the schema SQL commands.
db_engine = create_engine('sqlite://')
with db_engine.connect() as db_conn:
for cmd in db_schema_cmds:
db_conn.execute(text(cmd))
# Use automap to reflect the DB schema and generate ORM classes.
Base = automap_base()
Base.prepare(db_engine, reflect=True)
# Create aliases for the table classes generated.
User = Base.classes.users
Post = Base.classes.posts
session_factory = sessionmaker()
session_factory.configure(bind=db_engine)
# Add a user and a post to the DB.
session = session_factory()
new_user = User(name="John")
session.add(new_user)
session.commit()
new_post = Post(users=new_user, text='this is a test', timestamp=func.now())
session.add(new_post)
session.commit()
# Verify that the insertion worked.
new_user_id = session.execute(select([User])).fetchone()['u_id']
new_post_fk_user_id = session.execute(select([Post])).fetchone()['u_id']
assert new_user_id == new_post_fk_user_id
session.close()
运行此命令会提供以下回溯:
Traceback (most recent call last):
File "reproduce_InvalidRequestError.py", line 67, in <module>
session.commit()
File "C:\Python\Python35\lib\site-packages\sqlalchemy\orm\session.py", line 801, in commit
self.transaction.commit()
File "C:\Python\Python35\lib\site-packages\sqlalchemy\orm\session.py", line 392, in commit
self._prepare_impl()
File "C:\Python\Python35\lib\site-packages\sqlalchemy\orm\session.py", line 372, in _prepare_impl
self.session.flush()
File "C:\Python\Python35\lib\site-packages\sqlalchemy\orm\session.py", line 2019, in flush
self._flush(objects)
File "C:\Python\Python35\lib\site-packages\sqlalchemy\orm\session.py", line 2137, in _flush
transaction.rollback(_capture_exception=True)
File "C:\Python\Python35\lib\site-packages\sqlalchemy\util\langhelpers.py", line 60, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "C:\Python\Python35\lib\site-packages\sqlalchemy\util\compat.py", line 186, in reraise
raise value
File "C:\Python\Python35\lib\site-packages\sqlalchemy\orm\session.py", line 2107, in _flush
flush_context.finalize_flush_changes()
File "C:\Python\Python35\lib\site-packages\sqlalchemy\orm\unitofwork.py", line 395, in finalize_flush_changes
self.session._register_newly_persistent(other)
File "C:\Python\Python35\lib\site-packages\sqlalchemy\orm\session.py", line 1510, in _register_newly_persistent
instance_key = mapper._identity_key_from_state(state)
File "C:\Python\Python35\lib\site-packages\sqlalchemy\orm\mapper.py", line 2417, in _identity_key_from_state
for col in self.primary_key
File "C:\Python\Python35\lib\site-packages\sqlalchemy\orm\mapper.py", line 2417, in <listcomp>
for col in self.primary_key
File "C:\Python\Python35\lib\site-packages\sqlalchemy\orm\attributes.py", line 578, in get
value = state._load_expired(state, passive)
File "C:\Python\Python35\lib\site-packages\sqlalchemy\orm\state.py", line 474, in _load_expired
self.manager.deferred_scalar_loader(self, toload)
File "C:\Python\Python35\lib\site-packages\sqlalchemy\orm\loading.py", line 647, in load_scalar_attributes
"contain a full primary key." % state_str(state))
sqlalchemy.exc.InvalidRequestError: Instance <posts at 0x45d45f8> cannot be refreshed - it's not persistent and does not contain a full primary key.
如果我在 create_engine
调用中添加一个 echo=True
参数,我看到它为插入生成了以下SQL . 当我在DB浏览器中运行SQLite时,此SQL工作正常 .
INFO sqlalchemy.engine.base.Engine BEGIN (implicit)
INFO sqlalchemy.engine.base.Engine SELECT users.u_id AS users_u_id, users.name AS users_name FROM users WHERE users.u_id = ?
INFO sqlalchemy.engine.base.Engine (1,)
INFO sqlalchemy.engine.base.Engine INSERT INTO posts (timestamp, text, u_id) VALUES (CURRENT_TIMESTAMP, ?, ?)
INFO sqlalchemy.engine.base.Engine ('this is a test', 1)
INFO sqlalchemy.engine.base.Engine ROLLBACK
我还尝试从 Post()
删除 users
参数,而是在调用 session.add(new_post)
之前添加行 new_user.posts_collection.append(new_post)
,但这导致生成相同的SQL并发生相同的错误 .
如果我用新的整数PK列替换复合键,一切正常 . (虽然不是一个理想的解决方案,因为我使用 automap
的原因是为了反映现有的数据库,所以它是's preferable not to have to modify that DB'架构 . )
我发现了一个类似的问题,SQLAlchemy InvalidRequestError when using composite foreign keys,但这似乎与在表ORM类中使用继承有关,而解决方案依赖于定义ORM表类,而不是反映DB来生成它们 .
编辑:我原先假设这个问题与我的复合主键包含外键的事实有关 . 接受的答案表明,外键实际上并不是解决问题的因素 .
1 回答
问题实际上不是具有外键的复合主键,而
func.now()
传递为timestamp
,它是主键的一部分 . 由于SQLAlchemy不知道该值,因为它是在数据库中插入期间生成的,因此无法执行post-fetch;它不知道要取什么 . 如果相关数据库支持RETURNING
或类似,您可以执行此操作 . 请参阅triggered columns上的注释,该注释描述了这种情况 . Defaults / SQL Expressions中还介绍了主键值的预执行SQL .它与整数代理主键一起使用的原因是SQLite确实具有fetching the last inserted row id(一个整数主键列)的机制,SQLAlchemy可以使用该机制 .
要解决此问题,您可以使用Python中生成的时间戳
另一种解决方案是在反射期间覆盖
timestamp
列并提供func.now()
作为默认值 . 这将触发func.now()
的预执行 .在默认情况下,您在创建新实例时不需要(也不应该)提供
timestamp