我是烧瓶和炼金术的新手,决定通过使用烧瓶从事一个大型项目来学习。当我开始在类之间创建关系时。 每个类都位于单独的文件和文件夹中。我想要实现的逻辑是每个控制帐户只能分配一个项目,而一个项目可以分配给多个控制帐户,即一对一关系。
我不断收到以下错误消息 sqlalchemy.exc.InternalError: (pymysql.err.InternalError) (1054, "'field list'中的未知列'project_id'") [SQL: '插入控制帐户(代码、名称、预算、PMB_start
、PMB_finish
、PMU_start
、PMU_finish
、parent_id、project_id) 值(%s, %s, %s, %s, %s, %s)'] [参数: ('Root', 'Root', 1.0, datetime.date(2017, 12, 1), datetime.date(2017, 12, 31), datetime.date(2018, 12, 1), datetime.date(2019, 2, 27), 1, 1)]
我在控制帐户/模型中的代码.py
class ControlAccount(db.Model):
__tablename__='controlaccounts'
id = db.Column(db.Integer, primary_key =True)
code = db.Column(db.String(80))
name = db.Column(db.String(80))
budget = db.Column(db.Float)
PMB_start = db.Column(db.Date)
PMB_finish = db.Column(db.Date)
PMU_start = db.Column(db.Date)
PMU_finish = db.Column(db.Date)
parent_id = db.Column(db.Integer)
project_id = db.Column(db.Integer, db.ForeignKey('projects.id'))
def __init__(self, code, name, budget, PMB_start, PMB_finish, PMU_start, PMU_finish, parent_id, project):
self.code = code
self.name = name
self.parent_id = parent_id
#self.accounttype_id = accounttype.id
self.budget = budget
self.PMB_start = PMB_start
self.PMB_finish = PMB_finish
self.PMU_start = PMU_start
self.PMU_finish = PMU_finish
self.project_id = project.id
def __repr__(self):
return self.name
项目/模型文件代码为
class Project(db.Model):
__tablename__ ="projects"
id = db.Column(db.Integer, primary_key=True)
code = db.Column(db.String(80))
name = db.Column(db.String(80))
owner = db.Column(db.Integer, db.ForeignKey('users.id'))
description = db.Column(db.Text)
start = db.Column(db.DateTime)
finish = db.Column(db.DateTime)
status = db.Column(db.Boolean)
project = db.relationship('ControlAccount', backref='projects', lazy='dynamic',primaryjoin="Project.id == ControlAccount.project_id")
def __init__(self, code, name, description, owner, start, finish, status):
self.code = code
self.name = name
self.owner = owner
self.description = description
self.start = start
self.finish = finish
self.status = status
控制帐户代码的形式是
class ControlAccountForm(Form):
def get_projects():
return Project.query.all()
code = StringField('Code', [validators.Required()])
name = StringField('Type Name', [validators.Required()])
#accounttype = QuerySelectField('Account Type', query_factory= get_account_types)
#curve_id = QuerySelectField('Spread Profile', query_factory= get_spread_profile)
budget = FloatField('Budget', [validators.Required()])
PMB_start = DateField('Planned Start', [validators.Required()])
PMB_finish = DateField('Planned Finish', [validators.Required()])
PMU_start = DateField('Anticipated Start')
PMU_finish = DateField('Anticipated Finish')
#parent_id = QuerySelectField('Parent Accounts', query_factory= get_control_accounts)
parent_id = IntegerField('Parent ID')
project_id = QuerySelectField('Project', query_factory= get_projects)
最后是视图代码
@app.route('/newcontrolaccount', methods=['POST', 'GET'])
def newcontrolaccount():
form = ControlAccountForm()
if request.method == "POST" and form.validate():
#accounttype = form.accounttype.data
controlaccount = ControlAccount (code= form.code.data,
name= form.name.data,
#accounttype = form.accounttype.data,
budget = form.budget.data,
PMB_start = form.PMB_start.data,
PMB_finish = form.PMB_finish.data,
PMU_start = form.PMU_start.data,
PMU_finish = form.PMU_finish.data,
parent_id = form.parent_id.data,
#curve_id = form.curve_id.data,
project = form.project_id.data
)
db.session.add(controlaccount)
db.session.commit()
return redirect(url_for('ca_added'))
return render_template('controlaccounts/newaccount.html', form=form, action='new')
正如错误所说,数据库中缺少project_id
字段。使用数据库查看器查看是否已创建。否则,删除数据库并使用db.create_all()
重新创建它。
此外,应删除Project
类中的project
字段。这说不通。如果要获取相关控制帐户的反向引用,只需在控件帐户类中添加反向引用即可。有关详细信息,请参阅 http://docs.sqlalchemy.org/en/latest/orm/basic_relationships.html#one-to-one。
class ControlAccount(db.Model):
__tablename__='controlaccounts'
# ...
project_id = db.Column(db.Integer, db.ForeignKey('projects.id'))
project = relationship("Project", backref="control_account")