vendredi 1 janvier 2016

SQLAlchemy: Issue with One-to-One relationship object creation

I am new to SQLAlchemy and I was trying to run samples given in its documentation Basic Relationship Patterns - One to One Relationship. However as I try to instantiate the Parent class, I get into trouble.

Basically, what I have is:

class Parent(Base):
    __tablename__ = 'parent'
    id = Column(Integer, primary_key=True)
    child = relationship("Child", uselist=False, back_populates="parent")

class Child(Base):
    __tablename__ = 'child'
    id = Column(Integer, primary_key=True)
    parent_id = Column(Integer, ForeignKey('parent.id'))
    parent = relationship("Child", back_populates="child")

p1 = Parent()

The tables are made and are listed by .tables at sqlite> prompt, but at the line p1 = Parent() I receive this:

sqlalchemy.exc.ArgumentError: reverse_property 'parent' on relationship Parent.child references relationship Child.parent, which does not reference mapper Mapper|Parent|parent

This doesn't happen for one-to-many relationship and for this piece of code I get [ ] and None printed out as I expect:

class User(Base):
    __tablename__ = 'user'
    id = Column(Integer, primary_key=True)
    name = Column(String)

    addresses = relationship("Address", back_populates="user")

class Address(Base):
    __tablename__ = 'address'
    id = Column(Integer, primary_key=True)
    email = Column(String)
    user_id = Column(Integer, ForeignKey('user.id'))

    user = relationship("User", back_populates="addresses")


u1 = User()
a1 = Address()

print(u1.addresses)
print(a1.user_id)

So, I don't quite understand what the SQLAlchemy error message is trying to tell me.

Can anyone help please?

Aucun commentaire:

Enregistrer un commentaire