Hi list.

I guess I messed it.

I built an app on the premise I can build two different relationships
relying on the same field. My data model is basically as below. Class
names are roughly translated from Spanish. Please read comments in
Charge subclasses to grasp the problem.

class Affiliate(Base):

    id = Column(Integer, primary_key=True)
    ...

class AffiliateFamilyMember(Base)
    affiliate_id = Column(Integer, primary_key=True)
    family_member_no = Column(SmallInteger, primary_key=True)


class Charge(Base):

    id = Column(Integer, primary_key=True)
    affiliate_id = Column(Integer, ForeignKey('socios.id'), nullable=False)
    family_member_no = Column(SmallInteger)

    affiliate = relationship('Affiliate',
foreign_keys='Prestacion.affiliate_id')
    family_member = relationship('AffiliateFamilyMember',
            foreign_keys='[Prestacion.affiliate_id,
Prestacion.family_member_no]')
    ...


# Charge has a lot of subclasses like those
class LoanCharge(Charge):

    # Only affiliates can have loans.
    # affiliate attribute is set to the Affiliate instance
    # family_member attribute has no sense in this case
    ...


class PharmacyPurchase(Charge):
    # Any family member of affiliates can do farmacy purchases.    
    # family_member is set to the AffiliateFamilyMember instance
    # affiliate is a valid read-only attribute on persisted instances
    #    as Affiliate.id == AffiliateFamilyMember.affiliate_id


Being a bit awkward, It worked well and silently on SQLAlchemy 0.8.x and
0.9.x. I went into production using 0.9.x.

But when using 1.x versions I got:

    SAWarning: relationship 'Charge.family_member' will copy column
affiliate_family_members.affiliate_id to column       
    charges.affiliate_id, which conflicts with relationship(s):
'Charge.affiliate' (copies affiliates.id to charges.affiliate_id).
    Consider applying viewonly=True to read-only relationships, or
provide a primaryjoin condition marking writable columns
    with the foreign() annotation.

Besides being a warning, I'm not sure if this actually can broke
something (Some problems I got trying to use 1.0.x days made me think
so). So I tried to fix it, but it's not as easy as it seemed.

The gist is that in certain Charge subclasses I need affiliate being a
regular relationship, in other subclasses I need affiliate being a
"viewonly" relationship. But as all subclasses share the same mapper,
relationships from different subclasses clash one with each other:

    SAWarning: relationship 'PharmacyPurchase.family_member will copy
column family_members.affiliate_id to column
    charges.affiliate_id which conflicts with relationship(s):
'LoanCharge.affiliate' (copies affiliates.id to charges.affiliate_id).
    Consider applying viewonly=True to read-only relationships, or
provide a primaryjoin condition marking writable columns
    with the foreign() annotation.

I'm not sure there is really a solution, besides using separate fields
for both relationships, as that should be an endeavor I'd rather avoid.

Any suggestions?

Thanks in advance.

Greetings.

Julio.

-- 
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 post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to