问题
i tried to implement a simple sqlalchemy table relationship
so, I have below model:
class User(db.Model):
__tablename__ = "users"
UserID = db.Column(db.Integer, primary_key=True, autoincrement=True)
FirstName = db.Column(db.String(255), nullable=False)
LastName = db.Column(db.String(255), nullable=False)
#others column
Tokens = db.relationship('Token', backref='Token.UserID', primaryjoin='User.UserID==Token.UserID', lazy='dynamic')
#bla... other codes
and
class Token(db.Model):
__tablename__ = "user_tokens"
TokenID = db.Column(db.Integer, primary_key=True, autoincrement=True)
UserID = db.Column(db.Integer, db.ForeignKey('users.UserID'))
Token = db.Column(db.String(255), nullable=False)
#others column
#bla... other codes
and I do query:
user = User.query.filter(User.UserID).first()
problem: when i tried to access:
print(user.Tokens.UserID)
# or
print(user.Tokens.Token)
I always received:
'AppenderBaseQuery' object has no attribute 'UserID' / 'Token'
What am I missing here?
来源:https://stackoverflow.com/questions/44607363/sqlalchemy-1-to-many-relationship