Flask-SQLalchemy - 从Sub类中的Parent类获取关系参数

时间:2016-04-18 06:31:22

标签: python flask sqlalchemy flask-sqlalchemy

使用SQLalchemy和Flask-SQLAlchemy,我可以设置以下用户类和子类 Customer Provider :< / p>

dotplot(rate ~ pressure | Subject, data="Dialyzer")

然后我可以创建一个新客户:

class User(Base):
    __tablename__ = "user"
    id = Column(Integer, primary_key=True)
    name = Column(String(50), unique=False)
    user_type = Column(String(32), nullable=False)

    __mapper_args__ = {
    'polymorphic_on':'user_type',
    'polymorphic_identity':'user_type'
    }


class Customer(User):
    __tablename__ = "customer"
    id = Column(Integer, ForeignKey('user.id'), primary_key=True)
    note_customer = Column(Integer)

    __mapper_args__ = {
    'polymorphic_identity':'customer'
    }

class Provider(User):
    __tablename__ = "provider"
    id = Column(Integer, ForeignKey('user.id'), primary_key=True)
    note_provider = Column(Integer)

    __mapper_args__ = {
    'polymorphic_identity':'provider'
    }


class Address(Base):
    __tablename__ = "address"
    id = Column(Integer, primary_key=True)
    number = Column(Integer)
    street = Column(String(100))
    user_id = Column(Integer, ForeignKey('user.id'))
    user = relationship(User)

但我想在创建期间为我的客户商标添加一个地址值,但我没有成功,也无法弄清楚如何做到这一点。 我想我的关系是错的:对我来说这是一个“一对一”类型,但是我试过的很多解决方案都给我错误,告诉地址参数不适用于客户对象类型

谢谢!

2 个答案:

答案 0 :(得分:1)

问题是你既不使用backref也不明确创建2个后填充关系。因此,User模型类不知道关系,并且在构造时不能将Address实例传递给它或子类。您收到的错误应该是:

TypeError: 'address' is an invalid keyword argument for Customer

使用backref

relationship参数
from sqlalchemy.orm import backref

class Address(Base):
    __tablename__ = "address"
    id = Column(Integer, primary_key=True)
    number = Column(Integer)
    street = Column(String(100))
    user_id = Column(Integer, ForeignKey('user.id'))
    user = relationship(User, backref=backref('address', uselist=False))

这会在User模型上创建关系属性地址,就像您手动添加它一样:

    address = relationship('Address', uselist=False, ...)

您需要使用backref()对象才能传递uselist=False,因为您需要one to one关系。

使用显式关系属性

class User(Base):
    __tablename__ = "user"
    id = Column(Integer, primary_key=True)
    name = Column(String(50), unique=False)
    user_type = Column(String(32), nullable=False)

    address = relationship('Address', uselist=False, back_populates='user')

class Address(Base):
    __tablename__ = "address"
    id = Column(Integer, primary_key=True)
    number = Column(Integer)
    street = Column(String(100))
    user_id = Column(Integer, ForeignKey('user.id'))
    user = relationship(User, back_populates='address')

back_populates 参数将建立事件侦听器,这些侦听器将镜像类之间的属性操作。

通过适当的关系配置,可以像这样创建一个带地址的新Customer

u = Customer(name='Mark', user_type='customer', note_customer=15,
             address=Address(number=221, street='Baker Street'))
session.add(u)
session.commit()

或分开的步骤:

u = Customer(name='Mark', user_type='customer', note_customer=15)
u.address = Address(number=221, street='Baker Street')
session.add(u)
session.commit()

答案 1 :(得分:0)

错误是因为您的客户类未定义为具有地址。

您可以简单地添加另外几行来添加马克的地址,并将关系与马克联系起来。

mark_address = Address(number='123', street='Main St', user = u)
db.session.add(mark_address)
db.session.commit()