sqlalchemy自关联一对多、多对一

from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()
metadata = Base.metadata

class User(Base):
    __tablename__ = 'user'

    id = Column(BigInteger, primary_key=True)
    pid = Column(ForeignKey('user.id', ondelete='CASCADE', onupdate='CASCADE'), index=True)
    parent = relationship('User', remote_side=[id])
    children = relationship('User', remote_side=[pid])

参考资料:

https://blog.csdn.net/kzl_knight/article/details/103164537 

 

你可能感兴趣的:(sqlalchemy,python,python,sqlalchemy)