I have a lot of model classes with ralations between them with a CRUD interface to edit. The problem is that some objects can’t be deleted since there are other objects refering to them. Sometimes I can setup ON DELETE rule to handle this case, but in most cases I don’t want automatic deletion of related objects till they are unbound manually. Anyway, I’d like to present editor a list of objects refering to currently viewed one and highlight those that prevent its deletion due to FOREIGN KEY constraint. Is there a ready solution to automatically discover referers?
Update
The task seems to be quite common (e.g. django ORM shows all dependencies), so I wonder that there is no solution to it yet.
There are two directions suggested:
- Enumerate all relations of current object and go through their
backref. But there is no guarantee that all relations havebackrefdefined. Moreover, there are some cases whenbackrefis meaningless. Although I can define it everywhere I don’t like doing this way and it’s not reliable. - (Suggested by van and stephan) Check all tables of
MetaDataobject and collect dependencies from theirforeign_keysproperty (the code of sqlalchemy_schemadisplay can be used as example, thanks to stephan’s comments). This will allow to catch all dependencies between tables, but what I need is dependencies between model classes. Some foreign keys are defined in intermediate tables and have no models corresponding to them (used assecondaryin relations). Sure, I can go farther and find related model (have to find a way to do it yet), but it looks too complicated.
Solution
Below is a method of base model class (designed for declarative extention) that I use as solution. It is not perfect and doesn’t meet all my requirements, but it works for current state of my project. The result is collected as dictionary of dictionaries, so I can show them groupped by objects and their properties. I havn’t decided yet whether it’s good idea, since the list of referers sometimes is huge and I’m forced to limit it to some reasonable number.
def _get_referers(self):
db = object_session(self)
cls, ident = identity_key(instance=self)
medatada = cls.__table__.metadata
result = {}
# _mapped_models is my extension. It is collected by metaclass, so I didn't
# look for other ways to find all model classes.
for other_class in medatada._mapped_models:
queries = {}
for prop in class_mapper(other_class).iterate_properties:
if not (isinstance(prop, PropertyLoader) and \
issubclass(cls, prop.mapper.class_)):
continue
query = db.query(prop.parent)
comp = prop.comparator
if prop.uselist:
query = query.filter(comp.contains(self))
else:
query = query.filter(comp==self)
count = query.count()
if count:
queries[prop] = (count, query)
if queries:
result[other_class] = queries
return result
Thanks to all who helped me, especially stephan and van.
SQL: I have to absolutely disagree with S.Lott’ answer.
I am not aware of out-of-the-box solution, but it is definitely possible to discover all the tables that have ForeignKey constraints to a given table. One needs to use properly the
INFORMATION_SCHEMAviews such asREFERENTIAL_CONSTRAINTS,KEY_COLUMN_USAGE,TABLE_CONSTRAINTS, etc. See SQL Server example. With some limitations and extensions, most versions of new relational databases supportINFORMATION_SCHEMAstandard. When you have all the FK information and the object (row) in the table, it is a matter of running fewSELECTstatements to get all other rows in other tables that refer to given row and prevent it from being deleted.SqlAlchemy: As noted by stephan in his comment, if you use
ormwithbackreffor relations, then it should be quite easy for you to get the list of parent objects that keep reference to the object you are trying to delete, because those objects are basically mapped properties of your object (child1.Parent).If you work with
Tableobjects of sql alchemy (or not always usebackreffor relations), then you would have to get values offoreign_keysfor all the tables, and then for all thoseForeignKeys callreferences(...)method, providing your table as a parameter. In this way you will find all the FKs (and tables) that have reference to the table your object maps to. Then you can query all the objects that keep reference to your object by constructing the query for each of those FKs.