在 Flask、SqlAlchemy 中获取 "insert_id" 一对一关系
Get "insert_id" for one to one relationship in Flask, SqlAlchemy
我正在尝试通过 sqlalchemy 插入一个新行。父项 table(里程碑)有一个名为 Funding 的子项 table。两个 table 通过名为 milestone_id 的列共享关系。这是一对一的关系。
我已经查过了,但我不知道如何在 Funding table 中插入新行时引用 milestone_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)
在资助 table 中插入资助信息时,我如何告诉 SqlAlchemy 使用里程碑 table 自动生成的 milestone_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)
有什么想法吗?
在 db.session.add(..)
调用后添加 db.session.flush()
。这将导致 INSERT 被发送到数据库,之后您将可以访问该 ID。
我找不到确切的解决方案。如果有人想知道,我最终通过直接执行 SQL 来解决它。这并不理想,但它现在可以完成工作。我最终一次插入一行,这是下面的代码:
@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 = time.mktime(datetime.datetime.strptime(milestone_date, '%B %d, %Y').timetuple())
sql = "INSERT INTO ******** (`milestone_id`,`company_id`, `milestone_date`) VALUES ('','{}','{}')".format(company_id, milestone_date_final)
result = db.engine.execute(sql)
milestone_id = result.lastrowid
sql = "INSERT INTO ****** (`funding_id`,`funding_type`, `funding_message`, `funding_amount`, `milestone_source`, `company_id`, `milestone_id`, `user_id`, `funding_timestamp`) VALUES ('','{}','{}','{}','{}','{}','{}','1', '{}')".format(funding_type, funding_message, funding_amount, milestone_source, company_id, milestone_id, milestone_date_final)
result = db.engine.execute(sql)
output = result.lastrowid
return jsonify(result=output)
我正在尝试通过 sqlalchemy 插入一个新行。父项 table(里程碑)有一个名为 Funding 的子项 table。两个 table 通过名为 milestone_id 的列共享关系。这是一对一的关系。
我已经查过了,但我不知道如何在 Funding table 中插入新行时引用 milestone_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)
在资助 table 中插入资助信息时,我如何告诉 SqlAlchemy 使用里程碑 table 自动生成的 milestone_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)
有什么想法吗?
在 db.session.add(..)
调用后添加 db.session.flush()
。这将导致 INSERT 被发送到数据库,之后您将可以访问该 ID。
我找不到确切的解决方案。如果有人想知道,我最终通过直接执行 SQL 来解决它。这并不理想,但它现在可以完成工作。我最终一次插入一行,这是下面的代码:
@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 = time.mktime(datetime.datetime.strptime(milestone_date, '%B %d, %Y').timetuple())
sql = "INSERT INTO ******** (`milestone_id`,`company_id`, `milestone_date`) VALUES ('','{}','{}')".format(company_id, milestone_date_final)
result = db.engine.execute(sql)
milestone_id = result.lastrowid
sql = "INSERT INTO ****** (`funding_id`,`funding_type`, `funding_message`, `funding_amount`, `milestone_source`, `company_id`, `milestone_id`, `user_id`, `funding_timestamp`) VALUES ('','{}','{}','{}','{}','{}','{}','1', '{}')".format(funding_type, funding_message, funding_amount, milestone_source, company_id, milestone_id, milestone_date_final)
result = db.engine.execute(sql)
output = result.lastrowid
return jsonify(result=output)