How to specify relationships using declarative SQLAlchemy syntax?

I cannot find the correct documentation on how to specify relationships using the declarative syntax of SQLAlchemy .. Is it not supported? That is, should the "traditional" syntax be used?
I am looking for a way to define relationships at a higher level, avoiding the need to bother with foreign keys, etc. I would just declare “addresses = OneToMany (Address)” and let the structure handle the details. I know that Elixir can do this, but I was wondering if "simple" SQLA could do this.
Thank you for your help!

+3
source share
2 answers

, , , , :

class User(Base):
    __tablename__ = 'users'

    id = Column('id', Integer, primary_key=True)
    addresses = relation("Address", backref="user")

class Address(Base):
    __tablename__ = 'addresses'

    id = Column('id', Integer, primary_key=True)
    user_id = Column('user_id', Integer, ForeignKey('users.id'))
+3

" " . , "OneToMany", , .

class Address(Base):
    __tablename__ = 'addresses'

    id = Column(Integer, primary_key=True)
    email = Column(String(50))
    user_id = Column(Integer, ForeignKey('users.id'))
0

Source: https://habr.com/ru/post/1698920/


All Articles