1

I would like to use the dependent_objects() method to fetch all the relation instances in a database referencing a particular instance in a given relation. I tried the following:

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)

But it returns an exception:

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'>

Apparently, dependent_objects() is getting an unknown object at some point, but not clear which.

Luís de Sousa
  • 5,765
  • 11
  • 49
  • 86

1 Answers1

-2

dependent_objects() is documented to take an "SQLAlchemy declarative model object" (i.e. class User(base)).

Consider a User object is referenced in various articles and also in various orders. Getting all these dependent objects is as easy as: dependent_objects(user) [..]

The common use case is checking for all restrict dependent objects before deleting parent object and inform the user if there are dependent objects with ondelete=’RESTRICT’ foreign keys.

I don't understand why you're trying to inspect a query with a WHERE condition, but this kind of object is not supported - so it ends up in this part of get_mapper, which is where <class 'type'> you see in the exception comes from.

Community
  • 1
  • 1
Nickolay
  • 31,095
  • 13
  • 107
  • 185