1

我想使用该dependent_objects()方法来获取数据库中引用给定关系中特定实例的所有关系实例。我尝试了以下方法:

uri = "sqlite:///data.sqlite"

def getRecord(relName, uuid):

    engine = create_engine(uri, echo=False)
    listen(engine, 'connect', __load_spatialite)

    Session = sessionmaker(bind=engine)
    Session.configure(bind=engine)
    session = Session()

    metadata = MetaData()
    relation = Table(relName, metadata, autoload=True, autoload_with=engine)

    instance = session.query(relation).filter(text("id LIKE '" + uuid + "'"))
    dependent_objects(instance)

但它返回一个异常:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3/dist-packages/sqlalchemy_utils/functions/foreign_keys.py", line 263, in dependent_objects
    foreign_keys = get_referencing_foreign_keys(obj)
  File "/usr/lib/python3/dist-packages/sqlalchemy_utils/functions/foreign_keys.py", line 86, in get_referencing_foreign_keys
    tables = get_tables(mixed)
  File "/usr/lib/python3/dist-packages/sqlalchemy_utils/functions/orm.py", line 416, in get_tables
    mapper = get_mapper(mixed)
  File "/usr/lib/python3/dist-packages/sqlalchemy_utils/functions/orm.py", line 301, in get_mapper
    return sa.inspect(mixed)
  File "/usr/lib/python3/dist-packages/sqlalchemy/inspection.py", line 75, in inspect
    type_)
sqlalchemy.exc.NoInspectionAvailable: No inspection system is available for object of type <class 'type'>

显然,dependent_objects()在某个时候得到了一个未知的对象,但不清楚是哪个。

4

1 回答 1

-2

dependent_objects()记录为采用“SQLAlchemy 声明性模型对象”( class User(base))。

考虑在各种文章中以及以各种顺序引用 User 对象。获取所有这些依赖对象就像:dependent_objects(user)[..]

常见的用例是在删除父对象之前检查所有限制依赖对象,并通知用户是否存在具有 ondelete='RESTRICT' 外键的依赖对象。

我不明白您为什么要尝试使用 WHERE 条件检查查询,但是不支持这种对象-因此它最终出现在 的这一部分中get_mapper,这就是<class 'type'>您在异常中看到的来源。

于 2019-08-16T20:20:26.067 回答