If I had time to respond yesterday, I would have said the same thing as 
Simon.

Your database model leverages two separate parts of SQLAlchemy:

* SqlAlchemy ORM (Left side of the docs https://docs.sqlalchemy.org/en/13/ )
* SqlAlchemy Core (Right side of the docs https://docs.sqlalchemy.org/en/13/
 )

There is nothing wrong with that setup; the two are often used together and 
the docs recommend that in many situations!

However... the way you use and want to query the relationship table is more 
suited to redefining the permissions tables from SQLAlchemy Core objects 
(which are created by invoking `db.Table()`) into SQLAlchemy ORM classes 
that inherit from `db.model`.  

It is possible to keep these objects in "Core" and query them as-is, but 
that will have some tradeoffs:

* your application code will mix ORM and Core, which can cause some 
maintenance headaches
* changes to ORM and Core are independent of each other and may cause 
issues like race conditions if they overlap. For example, changes made to 
the database via Core would not necessarily appear to ORM 
objects/relationships if they are already loaded. 

There is no right way or wrong way here. Given your familiarity with this 
library though, I personally suggest keeping everything in the ORM.

Going to the ORM docs, your setup right now is roughly in line with a "Many 
to Many" setup that leverages an `association_table` (
https://docs.sqlalchemy.org/en/13/orm/basic_relationships.html#many-to-many)

However, your association_table is more than just primary keys joining the 
two sides of the relationship - it has other fields - and you want to be 
querying it directly.  That is more in line with the "Association Object" 
pattern (
https://docs.sqlalchemy.org/en/13/orm/basic_relationships.html#association-object
)

Altering your model to implementing the Association Object pattern is 
pretty straightforward and should be easy to do based on the examples in 
the docs. That should give you the flexibility you need.


-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/9121424d-8298-4018-9e95-1d4e312604e2%40googlegroups.com.

Reply via email to