Correction on wordings:

One parent table, two child tables, one foreign key from each child
pointing to the same field in parent with "one to one relationship" works
with no problem, but getting "AmbiguousForeignKeysError" as soon as adding
the second foreign key to child tables pointing to the second field in
parent. tried various combinations but none has worked so far. specifically
tring to add foreign_keys as suggested in error message.

Here is the code that throwing error.

class Parent(Base):
    __tablename__ = 'parent'

    field_one = Column(String(256),
                        unique=True,
                        nullable=False,
                        primary_key=True)

    field_two = Column(String(128),
                       nullable=False,
                       primary_key=True)

    p_child_one_fields = relationship("ChildOne",
                             uselist=False,
                             passive_deletes=True,
                             backref=backref("ref_to_parent_field_one",
                             foreign_keys="[ChildOne.field_one,
ChildOne.field_two]"),
                             cascade="all, delete-orphan")

    p_child_two_fields = relationship("ChildTwo",
                              uselist=False,
                              passive_deletes=True,
                              backref=backref("ref_to_parent_field_two",
                              foreign_keys="[ChildTwo.field_one,
ChildTwo.field_two]"),
                              cascade="all, delete-orphan")



class ChildOne(Base):
    __tablename__ = 'child_one'

    field_one = Column(String(256),
                        ForeignKey('parent.field_one',
                        onupdate="CASCADE",
                        ondelete='CASCADE'),
                        unique=True,
                        nullable=False,
                        primary_key=True)

    field_two = Column(String(256),
                        ForeignKey('parent.field_two',
                        onupdate="CASCADE",
                        ondelete='CASCADE'),
                        unique=True,
                        nullable=False,
                        primary_key=True)


class ChildTwo(Base):
    __tablename__ = 'child_two'

    field_one = Column(String(256),
                        ForeignKey('parent.field_one',
                        onupdate="CASCADE",
                        ondelete='CASCADE'),
                        unique=True,
                        nullable=False,
                        primary_key=True)

    field_two = Column(String(256),
                        ForeignKey('parent.field_two',
                        onupdate="CASCADE",
                        ondelete='CASCADE'),
                        unique=True,
                        nullable=False,
                        primary_key=True)


Any suggestion to fix the problem will be appreciated.
Do I need to have 4 relationships or can be done with two relationships ?

Thank you


On Sat, 30 May 2020 at 18:13, Sydo Luciani <sydo.luci...@gmail.com> wrote:

>
> One parent table, two child tables, two foreign keys pointing to a field
> in parent with "one to one relationship" works with no problem, but getting
> "AmbiguousForeignKeysError" as soon as adding the second foreignkey to
> child table. tried various combinations but none has worked so far.
> specifically tring to add foreign_keys as suggested in error message.
>
> Here is the code that throwing error.
>
> class Parent(Base):
>     __tablename__ = 'parent'
>
>     field_one = Column(String(256),
>                         unique=True,
>                         nullable=False,
>                         primary_key=True)
>
>     field_two = Column(String(128),
>                        nullable=False,
>                        primary_key=True)
>
>     p_child_one_field_one = relationship("ChildOne",
>                              uselist=False,
>                              passive_deletes=True,
>                              backref=backref("ref_to_parent_field_one",
>                              foreign_keys="[ChildOne.field_one,
> ChildOne.field_two]"),
>                              cascade="all, delete-orphan")
>
>     p_child_two_field_one = relationship("ChildTwo",
>                               uselist=False,
>                               passive_deletes=True,
>                               backref=backref("ref_to_parent_field_two",
>                               foreign_keys="[ChildTwo.field_one,
> ChildTwo.field_two]"),
>                               cascade="all, delete-orphan")
>
>
>
> class ChildOne(Base):
>     __tablename__ = 'child_one'
>
>     field_one = Column(String(256),
>                         ForeignKey('parent.field_one',
>                         onupdate="CASCADE",
>                         ondelete='CASCADE'),
>                         unique=True,
>                         nullable=False,
>                         primary_key=True)
>
>     field_two = Column(String(256),
>                         ForeignKey('parent.field_two',
>                         onupdate="CASCADE",
>                         ondelete='CASCADE'),
>                         unique=True,
>                         nullable=False,
>                         primary_key=True)
>
>
> class ChildTwo(Base):
>     __tablename__ = 'child_two'
>
>     field_one = Column(String(256),
>                         ForeignKey('parent.field_one',
>                         onupdate="CASCADE",
>                         ondelete='CASCADE'),
>                         unique=True,
>                         nullable=False,
>                         primary_key=True)
>
>     field_two = Column(String(256),
>                         ForeignKey('parent.field_two',
>                         onupdate="CASCADE",
>                         ondelete='CASCADE'),
>                         unique=True,
>                         nullable=False,
>                         primary_key=True)
>
>
> Any suggestion to fix the problem will be appreciated.
>
> Thank you
>

-- 
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/CAJspodhE_4%3DJcj_S5UE%2BrGvg3VGbxVGkRWnVgaaCGtFmWvXDUA%40mail.gmail.com.

Reply via email to