Python 从子级访问mixin成员变量(列)

Python 从子级访问mixin成员变量(列),python,sqlalchemy,flask-sqlalchemy,Python,Sqlalchemy,Flask Sqlalchemy,从子类访问父类成员变量id时遇到问题 class BaseModel: id = db.Column(db.Integer, primary_key=True) class User(db.Model, BaseModel): username = db.Column(db.String(35), nullable=False, unique=True) followed = db.relationship( 'User', secon

从子类访问父类成员变量
id
时遇到问题

class BaseModel:
    id = db.Column(db.Integer, primary_key=True)

class User(db.Model, BaseModel):
    username = db.Column(db.String(35), nullable=False, unique=True)

    followed = db.relationship(
        'User',
        secondary=followers,
        primaryjoin=(followers.c.follower_id == BaseModel.id),
        secondaryjoin=(followers.c.followed_id == BaseModel.id),
        backref=db.backref('followers', lazy='dynamic'),
        lazy='dynamic')
给我这个错误:

sqlalchemy.exc.InvalidRequestError: One or more mappers failed to initialize - can't proceed with initialization of other mappers. Triggering mapper: 'Mapper|User|user'. Original exception was: Could not locate any simple equality expressions involving locally mapped foreign key columns for primary join condition 'followers.follower_id = "<name unknown>"' on relationship User.followed.  Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or are annotated in the join condition with the foreign() annotation. To allow comparison operators other than '==', the relationship can be marked as viewonly=True.
sqlalchemy.exc.InvalidRequestError: Incorrect number of values in identifier to formulate primary key for query.get(); primary key columns are 'user.testing','user.id
给我这个错误:

sqlalchemy.exc.InvalidRequestError: One or more mappers failed to initialize - can't proceed with initialization of other mappers. Triggering mapper: 'Mapper|User|user'. Original exception was: Could not locate any simple equality expressions involving locally mapped foreign key columns for primary join condition 'followers.follower_id = "<name unknown>"' on relationship User.followed.  Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or are annotated in the join condition with the foreign() annotation. To allow comparison operators other than '==', the relationship can be marked as viewonly=True.
sqlalchemy.exc.InvalidRequestError: Incorrect number of values in identifier to formulate primary key for query.get(); primary key columns are 'user.testing','user.id

类定义的主体是它自己的作用域,引入的名称将构成类的名称空间。模型定义中的问题是,类
用户
的主体中未指定名称
id
,因此
关系定义中的联接引用内置函数。您也不能像这个问题中所示使用
BaseModel.id
,因为声明性元类会使用,所以它不会引用同一列

解决方案是使用惰性计算:传递可调用或Python可计算字符串作为联接:

followed = db.relationship(
    'User',
    secondary=followers,
    primaryjoin='followers.c.follower_id == User.id',   # Note: the joins are
    secondaryjoin='followers.c.followed_id == User.id', # passed as strings 
    backref=db.backref('followers', lazy='dynamic'),
    lazy='dynamic')
这是因为映射器的映射器间关系是在已声明映射类并首次使用之后配置的,除非使用显式配置

请注意,您也不能在可计算字符串中使用普通的
id
,而是必须使用
User
类来引用它,因为它稍后在与类主体完全不同的范围内进行计算

最后一个错误是以下代码的结果:

User.query.get(some_id)

在类主体中将
BaseModel.id
赋值给另一个名称(例如
testing
)会导致您的模型具有一个复合主键,由两个整数列组成,因此预期将接收两个整数元组,而不仅仅是一个整数。

NameError:未定义名称“User”您忘记创建“em string”,或者叫人。同样在将来,不要只是粘贴错误消息,而是提供一些上下文。