Company has an address_id column, which means each company only has a single address, doesn't it? ie. this is a many-to-one relationship, not a many-to-many?
Simon On Mon, Jan 27, 2020 at 11:28 PM Radek Krzak <radek.kr...@gmail.com> wrote: > > Hello, > > I have a ManyToMany relationship between two tables, although I am not using > "foreign keys" to establish it (this is a deliberate decision that I wouldn't > like to explain more at this point). However, I am struggling to define this > relationship using SQLAlchemy. At the very beginning, I found the primaryjoin > that I tried using with partial success. I managed to get it working with > backref (and with only one side being a list, which is not what I wanted), > but to be honest it was rather by accident. I keep missing the > information/examples of how exactly the foreign_keys and remote_side > arguments are and how to use them effectively. I went through every single SO > question on that topic and the documentation that mainly involves self joins > (that is understandable as my use case is rather uncommon). > > > class Address(Base): > __tablename__ = 'addresses' > id = Column(UUID, primary_key=True) > address_id = Column(String(8), nullable=False) > is_deleted = Column(Boolean, server_default=expression.false(), > nullable=False) > companies = relationship("Company", primaryjoin="...", > back_populates="addresses") > > > class Company(Base): > __tablename__ = 'companies' > id = Column(UUID, primary_key=True) > address_id = Column(String(8), nullable=False) > addresses = relationship("Address", primaryjoin="...", > back_populates="companies") > > > I would like a bidirectional relationship with using back_populates (I don't > like using backref as my models are often scattered across different modules > and I prefer to be explicit). I am not using primary keys on purpose as I > want to keep the historical addresses data just by marking address by deleted > (and keeping companies and address linked via address_id). Referential > integrity is enforced by the 3rd party provider where I import the data from, > so this is not an issue in my case. > > Any help on how I should define the relationship on both models is > appreciated. I really want to understand foreign_keys and remote_side because > at the moment I cannot seem to be able to wrap my head around, even after > reading > https://docs.sqlalchemy.org/en/13/orm/join_conditions.html#creating-custom-foreign-conditions > :( > > Thanks in advance > > -- > 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/e0c7d5f6-58fe-410e-b0af-1d60973d3ba9%40googlegroups.com. -- 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/CAFHwexeyqyHYFiMocYQyjqG0L2OUFebMeX1fTFoyMyOB%3DYwirw%40mail.gmail.com.