This is a simplified version of my models. Say I have a User model with many billing addresses and many shipping addresses. Both correspond to the Address model.
from sqlalchemy import ForeignKey
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column, relationship
class Base(DeclarativeBase):
pass
class Address(Base):
__tablename__ = "addresses"
id: Mapped[int] = mapped_column(primary_key=True)
address: Mapped[str]
user_id: Mapped[int] = mapped_column(ForeignKey("users.id"))
user = relationship("User")
class User(Base):
__tablename__ = "users"
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str]
billing_addresses = relationship("Address", back_populates="user")
shipping_addresses = relationship("Address", back_populates="user")
If I simply do, for example:
u = User()
Why do I get the following warning? And how do I resolve it?
:1: SAWarning: relationship ‘User.shipping_addresses’ will copy column users.id to column addresses.user_id, which conflicts with relationship(s): ‘User.billing_addresses’ (copies users.id to addresses.user_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=”billing_addresses”‘ to the ‘User.shipping_addresses’ relationship. (Background on this warning at: https://sqlalche.me/e/20/qzyx) (This warning originated from the
configure_mappers()
process, which was invoked automatically in response to a user-initiated operation.)
Thanks in advance!