How to create one-to-one accessor from one-to-many with extra join arguments with sqlalchemy?

canni

I have entity Client, it has one-to-many relation to entity Address. Address has column type, witch can be primary (int 1) or secondary (int 2).

I can access client addresses through client.addresses == [...] Now can I create accessors witch would act as one-to-one, eg: client.primary_address == Address(...) witch would evaluate to JOIN with extra JOIN arg: ON (client.id == address.client_id and address.type = 1)?

van

Following Specifying Alternate Join Conditions documentation, you can do:

class Client(Base):
    __tablename__ = "client"
    client_id = Column(Integer, primary_key=True)
    # ...

    addresses = relationship(Address, ...)

    primary_address = relationship(Address, uselist=False,
            primaryjoin = and_(Address.client_id == client_id, Address.type == 1),
            )
    secondary_address = relationship(Address, uselist=False,
            primaryjoin = and_(Address.client_id == client_id, Address.type == 2),
            )

This will allow you to access (read) both primary and secondary addresses as if it were 1-1 relationship. You can event set (write) it like below:

my_client.primary_address = Address(type=1, ...)

but you will have to ensure that the Address has proper value (1 or 2) for the type column which seems to be a technical field.


Side remark: type is reserved word in python.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Sqlalchemy one to many relationship join?

From Dev

One to many + one relationship in SQLAlchemy?

From Dev

How to create One-to-Many and Many-to-One Relationship from one class to another class in Java/IntelliJ

From Dev

SQLAlchemy one to many relationship, how to filter the collection

From Dev

SQLAlchemy one to many relationship, how to filter the collection

From Dev

mySQL JOIN on one to many

From Dev

SQLAlchemy join a "one to many table" and then filter on the joined table with a set of values

From Dev

sqlalchemy one to many join in flask app to get max date

From Dev

sqlalchemy one to many join in flask app to get max date

From Dev

How to join only one row from one-to-many relation where value is min or max?

From Dev

How to Inner Join a many to one on Like condition

From Dev

How to join one to many distinct in three table?

From Dev

How to query join one to many in hibernate HQL

From Dev

SQLAlchemy one to many query output

From Dev

Query One to Many Relationship SQLAlchemy

From Dev

One-to-many Flask | SQLAlchemy

From Dev

Query One to Many Relationship SQLAlchemy

From Dev

How to create only one table with SQLAlchemy?

From Dev

How to select one to one relations from many to many table

From Dev

Maven create many jars from one module

From Dev

SQL join maybe. One to one to many

From Dev

Join tables and choose one value of one to many

From Dev

SQLAlchemy One-To-One and One-To-Many at the same time (AmbiguousForeignKeysError)

From Dev

How to describe One To One relationships from Django ORM in SqlAlchemy?

From Dev

How to create an object in a one to many relationship

From Dev

How to create many http servers into one app?

From Dev

How to create an object in a one to many relationship

From Java

How does one ignore extra arguments passed to a data class?

From Dev

How does one ignore extra arguments passed to a data class?

Related Related

  1. 1

    Sqlalchemy one to many relationship join?

  2. 2

    One to many + one relationship in SQLAlchemy?

  3. 3

    How to create One-to-Many and Many-to-One Relationship from one class to another class in Java/IntelliJ

  4. 4

    SQLAlchemy one to many relationship, how to filter the collection

  5. 5

    SQLAlchemy one to many relationship, how to filter the collection

  6. 6

    mySQL JOIN on one to many

  7. 7

    SQLAlchemy join a "one to many table" and then filter on the joined table with a set of values

  8. 8

    sqlalchemy one to many join in flask app to get max date

  9. 9

    sqlalchemy one to many join in flask app to get max date

  10. 10

    How to join only one row from one-to-many relation where value is min or max?

  11. 11

    How to Inner Join a many to one on Like condition

  12. 12

    How to join one to many distinct in three table?

  13. 13

    How to query join one to many in hibernate HQL

  14. 14

    SQLAlchemy one to many query output

  15. 15

    Query One to Many Relationship SQLAlchemy

  16. 16

    One-to-many Flask | SQLAlchemy

  17. 17

    Query One to Many Relationship SQLAlchemy

  18. 18

    How to create only one table with SQLAlchemy?

  19. 19

    How to select one to one relations from many to many table

  20. 20

    Maven create many jars from one module

  21. 21

    SQL join maybe. One to one to many

  22. 22

    Join tables and choose one value of one to many

  23. 23

    SQLAlchemy One-To-One and One-To-Many at the same time (AmbiguousForeignKeysError)

  24. 24

    How to describe One To One relationships from Django ORM in SqlAlchemy?

  25. 25

    How to create an object in a one to many relationship

  26. 26

    How to create many http servers into one app?

  27. 27

    How to create an object in a one to many relationship

  28. 28

    How does one ignore extra arguments passed to a data class?

  29. 29

    How does one ignore extra arguments passed to a data class?

HotTag

Archive