【发布时间】:2015-06-11 07:56:07
【问题描述】:
我正在尝试通过 sqlalchemy 插入新行。父表 (Milestone) 有一个名为 Funding 的子表。两个表都通过一个名为milestone_id 的列共享关系。这是一对一的关系。
我已经查过了,但我不知道在 Funding 表中插入新行时如何引用里程碑 ID。父 ID 是自动增量的。我正在使用 Flask 和 SqlAlchemy。
型号:
class Milestone(db.Model):
__tablename__ = "**************"
milestone_id = db.Column(db.Integer, primary_key=True)
company_id = db.Column(db.Integer, db.ForeignKey('stlines_startups.company_id'))
milestone_date = db.Column(db.Integer)
snapshots = db.relationship('Snapshot', uselist=False, primaryjoin='Milestone.milestone_id==Snapshot.milestone_id', backref='milestone')
fundraising = db.relationship('Funding', uselist=False, primaryjoin='Milestone.milestone_id==Funding.milestone_id', backref='milestone')
def __init__(self, milestone_id, company_id, milestone_date, snapshots = [], fundraising = []):
self.milestone_id = milestone_id
self.company_id = company_id
self.milestone_date = milestone_date
self.snapshots = snapshots
self.fundraising = fundraising
class Funding(db.Model):
__tablename__ = "**************************"
funding_id = db.Column(db.Integer, primary_key=True)
funding_type = db.Column(db.Text)
funding_message = db.Column(db.Text)
funding_amount = db.Column(db.Integer)
milestone_source = db.Column(db.Text)
company_id = db.Column(db.Integer, db.ForeignKey('stlines_milestones.company_id'))
milestone_id = db.Column(db.Integer, db.ForeignKey('stlines_milestones.milestone_id'))
user_id = db.Column(db.Integer)
funding_timestamp = db.Column(db.Integer)
def __init__(self, funding_id, funding_type, funding_message, funding_amount, milestone_source, milestone_id, company_id, user_id, funding_timestamp):
self.funding_id = funding_id
self.funding_type = funding_type
self.funding_message = funding_message
self.funding_amount = funding_amount
self.milestone_source = milestone_source
self.milestone_id = milestone_id
self.company_id = company_id
self.user_id = user_id
self.funding_timestamp = funding_timestamp
炼金查询:
@app.route('/_add_funding')
def add_funding():
funding_type = request.args.get('funding_stage', '', type=str)
funding_message = request.args.get('funding_message', '', type=str)
funding_amount = request.args.get('funding_amount', 0, type=int)
milestone_source = request.args.get('milestone_source', '', type=str)
milestone_date = request.args.get('milestone_date', '', type=str)
company_id = request.args.get('company_id', '', type=int)
milestone_date_final = datetime.datetime.strptime(milestone_date, '%B %d, %Y')
''' In this line, I try to reference milestone_id with new_milestone.milestone_id, but nothing shows up in the database '''
new_funding = Funding('', funding_type=funding_type, funding_message=funding_message, funding_amount=funding_amount, milestone_source=milestone_source, company_id=company_id, milestone_id=new_milestone.milestone_id, user_id='1', funding_timestamp=milestone_date_final)
new_milestone = Milestone('', company_id=company_id, milestone_date=milestone_date_final, snapshots=None, fundraising=new_funding)
db.session.add(new_milestone)
output = new_milestone.milestone_id
db.session.commit()
return jsonify(result=output)
在资金表中插入资金信息时,如何告诉 SqlAlchemy 使用里程碑表中自动生成的里程碑 ID?这些应该是两个单独的查询吗?
更新:
我接受了 ThiefMaster 关于使用刷新功能的建议,但我仍然收到错误消息: UnboundLocalError:赋值前引用了局部变量“new_milestone”
这是更新后的代码:
@app.route('/_add_funding')
def add_funding():
funding_type = request.args.get('funding_stage', '', type=str)
funding_message = request.args.get('funding_message', '', type=str)
funding_amount = request.args.get('funding_amount', 0, type=int)
milestone_source = request.args.get('milestone_source', '', type=str)
milestone_date = request.args.get('milestone_date', '', type=str)
company_id = request.args.get('company_id', '', type=int)
milestone_date_final = datetime.datetime.strptime(milestone_date, '%B %d, %Y')
''' In this line, I try to reference milestone_id with new_milestone.milestone_id, but nothing shows up in the database '''
new_funding = Funding('', funding_type=funding_type, funding_message=funding_message, funding_amount=funding_amount, milestone_source=milestone_source, company_id=company_id, milestone_id=new_milestone.milestone_id, user_id='1', funding_timestamp=milestone_date_final)
new_milestone = Milestone('', company_id=company_id, milestone_date=milestone_date_final, snapshots=None, fundraising=new_funding)
db.session.add(new_milestone)
db.session.commit()
db.session.flush()
output = new_milestone.milestone_id
return jsonify(result=output)
有什么想法吗?
【问题讨论】:
-
commit()然后请求flush()是没有意义的,因为commit()会单独调用flush()。这个想法是,当您执行flush()时,SQLAlchemy 将填充 autoincrement 列的值,因此您可以访问 autoincrement 的值,以便在添加新子表时可以从子表中填充 parent_id。注意:当您向 SQLAlchemy 会话添加记录时,它不会(还)将记录写入数据库。在您触发提交或刷新后,它会立即执行所有操作。这有助于提高性能。