I have the following data base:
permission_id and contract_id have a relationship in the database
How can i using python remove the entry for example.. if permission_id = 2 and contract_id = 2 exists in the same entry as shown on line one in database, i want to be able to remove it from my database.
I have tried PermissionEntity.query.get(contract_id) and PermissionEntity.query.get(permission_id)
but doesnt seem to be working
please help
Correction to initial question:
could you let me know how i would remove this relationship if it was to be stored somewhere else?... at the moment I've realised its not stored in permission entity its a seperare table which has a relationship with contracts from a contract table and permisions from permission table (two separate tables).. in other words , access the relationship table and remove as I do not have an entity for it
-
In short I am looking to find out how can I find if a set of entries exist. so if permission_id and contract id is the same as what i send from my front end and if so , remove it from databaseT Hus– T Hus2020年05月22日 15:51:18 +00:00Commented May 22, 2020 at 15:51
1 Answer 1
Assuming you're instantiating SQLAlchemy as db then this will work:
result = db.session.query(
PermissionEntity
).filter(and_([PermissionEntity.contract_id==2, PermissionEntity.permission_id==2]
).all()
"""
result is a list object due to all()...if you only want to remove the
first instance, then use first() and you do not need to iterate through a
list like below.
"""
for i in result:
i.remove()
db.session.commit()
6 Comments
Explore related questions
See similar questions with these tags.