似乎没有来自另一个表的变量 - SQLAlchemy

时间:2014-05-23 10:21:26

标签: python flask sqlalchemy flask-sqlalchemy

我有这两个类但他们似乎无法相互联系。我得到的错误是:

OperationalError: (OperationalError) no such column: user_points.user_id

为什么会这样?

class User(db.Model):
    __tablename__ = "user"
    id = db.Column('user_id',db.Integer , primary_key=True)
    username = db.Column(db.String(20), unique=True , index=True)
    password = db.Column(db.String(20))
    firstname = db.Column(db.String(20))
    surname = db.Column(db.String(20))
    email = db.Column(db.String(100))
    dt_joined = db.Column(db.DateTime, default=db.func.now())
    points_rel = db.relationship('User_points',backref='pointers',lazy='dynamic')

 # User Points table
 class User_points(db.Model):
    __tablename__ = "user_points"
    id = db.Column('user_points_id',db.Integer, primary_key = True)
    user_id = db.Column('user_id',db.Integer, db.ForeignKey('user.user_id'))
    fixture_id = db.Column('fixture_id',db.Integer, db.ForeignKey('fixture.fixture_id'))
    fixture_prediction_id = db.Column('fixture_prediction_id',db.Integer, \
                            db.ForeignKey('fixture_prediction.fixture_id'))    
    points = db.Column('points',db.Integer)

任何人的想法???这是我的头脑:(

2 个答案:

答案 0 :(得分:0)

User班级尝试定义类似

points_rel = db.relationship('user_points',backref='user',lazy='dynamic')

而不是使用表名而不是类名。希望能解决你的问题。

OR

签出此代码

class User(db.Model):
    __tablename__ = "user"
    id = db.Column(db.Integer , primary_key=True)
    username = db.Column(db.String(20), unique=True , index=True)
    password = db.Column(db.String(20))
    firstname = db.Column(db.String(20))
    surname = db.Column(db.String(20))
    email = db.Column(db.String(100))
    dt_joined = db.Column(db.DateTime, default=db.func.now())
    points_rel = db.relationship('User_points', backref='user', lazy='dynamic')

# User Points table
class User_points(db.Model):
    __tablename__ = "user_points"
    id = db.Column(db.Integer, primary_key = True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    fixture_id = db.Column(db.Integer, db.ForeignKey('fixture.fixture_id'))
    fixture_prediction_id = db.Column(db.Integer, \
                            db.ForeignKey('fixture_prediction.fixture_id'))    
    points = db.Column(db.Integer)

答案 1 :(得分:0)

我做的是放下桌子并重新创建它。看起来元数据存在一些问题,当我执行.keys()时列就在那里了,虽然当我在select语句中专门命名它时它无法找到它。我无法解释为什么,但放弃桌子并重新创建它有效。