It's probably worth noting I can narrow it down to a single warning with
the following snippet and it's still unclear to me how to resolve this:

class Association(Base):
    __tablename__ = 'association'
    left_id = Column(ForeignKey('left.id'), primary_key=True)
    right_id = Column(ForeignKey('right.id'), primary_key=True)
    extra_data = Column(String(50))

    parent = relationship('Parent')

class Parent(Base):
    __tablename__ = 'left'
    id = Column(Integer, primary_key=True)
    children = relationship('Child', secondary=Association.__table__)

class Child(Base):
    __tablename__ = 'right'
    id = Column(Integer, primary_key=True)

foo.py:24: SAWarning: relationship 'Parent.children' will copy column
> left.id to column association.left_id, which conflicts with
> relationship(s): 'Association.parent' (copies left.id to
> association.left_id). If this is not the intention, consider if these
> relationships should be linked with back_populates, or if viewonly=True
> should be applied to one or more if they are read-only. For the less common
> case that foreign key constraints are partially overlapping, the
> orm.foreign() annotation can be used to isolate the columns that should be
> written towards.   To silence this warning, add the parameter
> 'overlaps="parent"' to the 'Parent.children' relationship. (Background on
> this error at: https://sqlalche.me/e/14/qzyx)


On Wed, Mar 9, 2022 at 4:31 PM Michael Merickel <mmeri...@gmail.com> wrote:

> I have looked at the couple examples in the docs (many-to-many, and
> association table) and have noticed that my codebase has a slightly
> different pattern which is causing warnings when upgrading to 1.4. I'm
> trying to figure out the best pattern to accomplish what I've been doing
> which doesn't match the docs exactly.
>
> In the below example you can see that there are backrefs on all of the
> links, and that there are backrefs from the link table to the related
> objects, as well as a secondary link from Parent to Child via
> Parent.children and Child.parents.
>
> There seem to be several options and I'm struggling to figure out what the
> solution should be to maintain the behavior with all of the following
> relationships working:
>
> - Parent.children
> - Parent.child_links
> - Child.parents
> - Child.parent_links
> - Association.parent
> - Association.child
>
> Code and warnings are below:
>
> from sqlalchemy import Column, ForeignKey, String, Integer
> from sqlalchemy.orm import configure_mappers, relationship
> from sqlalchemy.ext.declarative import declarative_base
>
> Base = declarative_base()
>
> class Association(Base):
>     __tablename__ = 'association'
>     left_id = Column(ForeignKey('left.id'), primary_key=True)
>     right_id = Column(ForeignKey('right.id'), primary_key=True)
>     extra_data = Column(String(50))
>
>     parent = relationship('Parent', backref='child_links')
>     child = relationship('Child', backref='parent_links')
>
> class Parent(Base):
>     __tablename__ = 'left'
>     id = Column(Integer, primary_key=True)
>     children = relationship('Child', secondary=Association.__table__,
> backref='parents')
>
> class Child(Base):
>     __tablename__ = 'right'
>     id = Column(Integer, primary_key=True)
>
> configure_mappers()
>
> foo.py:25: SAWarning: relationship 'Child.parents' will copy column
>> right.id to column association.right_id, which conflicts with
>> relationship(s): 'Association.child' (copies right.id to
>> association.right_id), 'Child.parent_links' (copies right.id to
>> association.right_id). If this is not the intention, consider if these
>> relationships should be linked with back_populates, or if viewonly=True
>> should be applied to one or more if they are read-only. For the less common
>> case that foreign key constraints are partially overlapping, the
>> orm.foreign() annotation can be used to isolate the columns that should be
>> written towards.   To silence this warning, add the parameter
>> 'overlaps="child,parent_links"' to the 'Child.parents' relationship.
>> (Background on this error at: https://sqlalche.me/e/14/qzyx)
>>   configure_mappers()
>> foo.py:25: SAWarning: relationship 'Child.parents' will copy column
>> left.id to column association.left_id, which conflicts with
>> relationship(s): 'Association.parent' (copies left.id to
>> association.left_id), 'Parent.child_links' (copies left.id to
>> association.left_id). If this is not the intention, consider if these
>> relationships should be linked with back_populates, or if viewonly=True
>> should be applied to one or more if they are read-only. For the less common
>> case that foreign key constraints are partially overlapping, the
>> orm.foreign() annotation can be used to isolate the columns that should be
>> written towards.   To silence this warning, add the parameter
>> 'overlaps="child_links,parent"' to the 'Child.parents' relationship.
>> (Background on this error at: https://sqlalche.me/e/14/qzyx)
>>   configure_mappers()
>> foo.py:25: SAWarning: relationship 'Parent.children' will copy column
>> left.id to column association.left_id, which conflicts with
>> relationship(s): 'Association.parent' (copies left.id to
>> association.left_id), 'Parent.child_links' (copies left.id to
>> association.left_id). If this is not the intention, consider if these
>> relationships should be linked with back_populates, or if viewonly=True
>> should be applied to one or more if they are read-only. For the less common
>> case that foreign key constraints are partially overlapping, the
>> orm.foreign() annotation can be used to isolate the columns that should be
>> written towards.   To silence this warning, add the parameter
>> 'overlaps="child_links,parent"' to the 'Parent.children' relationship.
>> (Background on this error at: https://sqlalche.me/e/14/qzyx)
>>   configure_mappers()
>> foo.py:25: SAWarning: relationship 'Parent.children' will copy column
>> right.id to column association.right_id, which conflicts with
>> relationship(s): 'Association.child' (copies right.id to
>> association.right_id), 'Child.parent_links' (copies right.id to
>> association.right_id). If this is not the intention, consider if these
>> relationships should be linked with back_populates, or if viewonly=True
>> should be applied to one or more if they are read-only. For the less common
>> case that foreign key constraints are partially overlapping, the
>> orm.foreign() annotation can be used to isolate the columns that should be
>> written towards.   To silence this warning, add the parameter
>> 'overlaps="child,parent_links"' to the 'Parent.children' relationship.
>> (Background on this error at: https://sqlalche.me/e/14/qzyx)
>>   configure_mappers()
>
>
> Thanks!
>
> --
>
> Michael
>


-- 

Michael

-- 
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/CAKdhhwHi-aWQ_ceZ9VrDVzy1B3RS-RCXZ04vb3mKcuwkh-YFkQ%40mail.gmail.com.

Reply via email to