如何在flask-sqlalchemy中插入外键值,这是另一个表的主键值?

时间:2017-10-06 08:05:36

标签: python database flask sqlalchemy flask-sqlalchemy

我现在有两个表,我正在尝试在这两个表之间建立一对多关系,但我不知道如何在表2中插入外键列。

这是表1和2数据库的代码:

class Calc(db.Model):
  __tablename__ = 'calc'
  id = db.Column(db.Integer, primary_key=True)
  pjt = db.Column(db.String(100))
  cse = db.Column(db.Integer)
  rmk = db.Column(db.String(100))

  gor = db.Column(db.Float(20))
  og = db.Column(db.Float(20))
  gg = db.Column(db.Float(20))
  temp = db.Column(db.Float(20))
  press = db.Column(db.Float(20))
  h2s = db.Column(db.Float(20))
  co2 = db.Column(db.Float(20))
  n2 = db.Column(db.Float(20))
  ppm = db.Column(db.Float(20))
  cor1 = db.Column(db.String(100))
  cor2 = db.Column(db.String(100))
  cor3 = db.Column(db.String(100))
  cor4 = db.Column(db.String(100))

  pb = db.Column(db.Float(20))
  rs = db.Column(db.Float(20))
  bo = db.Column(db.Float(20))
  co = db.Column(db.Float(20))
  uo = db.Column(db.Float(20))
  po = db.Column(db.Float(20))
  z = db.Column(db.Float(20))
  bg = db.Column(db.Float(20))
  pg = db.Column(db.Float(20))
  ug = db.Column(db.Float(20))
  bw = db.Column(db.Float(20))
  uw = db.Column(db.Float(20))
  pw = db.Column(db.Float(20))
  cw = db.Column(db.Float(20))
  iow = db.Column(db.Float(20))
  iog = db.Column(db.Float(20))
  iwg = db.Column(db.Float(20))

  process4 = db.relationship('Matching', backref='case', lazy='dynamic')

class Matching(db.Model):
  id = db.Column(db.Integer,primary_key=True)
  proc_id = db.Column(db.Integer, db.ForeignKey('calc.id'))

  pjt = db.Column(db.String(100))
  cse = db.Column(db.Integer)

  temp = db.Column(db.Float(20))
  pb = db.Column(db.Float(20))
  press = db.Column(db.Float(20))
  rs = db.Column(db.Float(20))
  bo = db.Column(db.Float(20))
  uo = db.Column(db.Float(20))

  pbG1 = db.Column(db.Float(20))
  pbG2 = db.Column(db.Float(20))
  pbS1 = db.Column(db.Float(20))
  pbS2 = db.Column(db.Float(20))
  pbVB1 = db.Column(db.Float(20))
  pbVB2 = db.Column(db.Float(20))
  pbP1 = db.Column(db.Float(20))
  pbP2 = db.Column(db.Float(20))
  pbAM1 = db.Column(db.Float(20))
  pbAM2 = db.Column(db.Float(20))

  rsG1 = db.Column(db.Float(20))
  rsG2 = db.Column(db.Float(20))
  rsS1 = db.Column(db.Float(20))
  rsS2 = db.Column(db.Float(20))
  rsVB1 = db.Column(db.Float(20))
  rsVB2 = db.Column(db.Float(20))
  rsP1 = db.Column(db.Float(20))
  rsP2 = db.Column(db.Float(20))
  rsAM1 = db.Column(db.Float(20))
  rsAM2 = db.Column(db.Float(20))

  boG1 = db.Column(db.Float(20))
  boG2 = db.Column(db.Float(20))
  boS1 = db.Column(db.Float(20))
  boS2 = db.Column(db.Float(20))
  boVB1 = db.Column(db.Float(20))
  boVB2 = db.Column(db.Float(20))
  boP1 = db.Column(db.Float(20))
  boP2 = db.Column(db.Float(20))
  boAM1 = db.Column(db.Float(20))
  boAM2 = db.Column(db.Float(20))

  uoBG1 = db.Column(db.Float(20))
  uoBG2 = db.Column(db.Float(20))
  uoBL1 = db.Column(db.Float(20))
  uoBL2 = db.Column(db.Float(20))

这是保存到数据库中的代码:

db.session.add(Matching(temp=mtemp, pb=mbppress, press=mpress, rs=mrrs, 
bo=mbbo,uo=muo, pbG1=session['mpb1'], pbG2=session['cpb1'], 
pbS1=session['mpb2'], pbS2=session['cpb2'],pbVB1=session['mpb3'], 
pbVB2=session['cpb3'], pbP1=session['mpb4'], pbP2=session['cpb4'],
pbAM1=session['mpb5'],pbAM2=session['cpb5'],rsG1=session['mrs1'], 
rsG2=session['crs1'], rsS1=session['mrs2'], rsS2=session['crs2'],
rsVB1=session['mrs3'], rsVB2=session['crs3'], rsP1=session['mrs4'], 
rsP2=session['crs4'],rsAM1=session['mrs5'], rsAM2=session['crs5'],
boG1=session['mbo1'], boG2=session['cbo1'], boS1=session['mbo2'], 
boS2=session['cbo2'],boVB1=session['mbo3'], boVB2=session['cbo3'], 
boP1=session['mbo4'], boP2=session['cbo4'],boAM1=session['mbo5'], 
boAM2=session['cbo5'],uoBG1=session['muo1'], uoBG2=session['cuo1'],                                      
uoBL1=session['muo2'],uoBL2=session['cuo2'], pjt=exf.pjt.data.pjt, 
cse=exf.cse.data.cse))

db.session.commit()

这是表1和表2的图片:

表1 Table 1

表2 Table 2

这是我的Calc实例:

db.session.add_all([Calc(gor=ingor, og=inog, gg=ingg, temp=intemp, 
press=inpress, h2s=inh2s, co2=inco2,
n2=inn2, ppm=inppm, cor1=cor1, cor2=cor2, cor3=cor3, cor4=cor4, 
pb=session['xpb'], rs=session['xrs'], bo=session['xbo'], co=session['xco'], 
uo=session['xuo'], po=session['xpo'],
z=session['xz'], bg=session['xbg'], pg=session['xpg'], ug=session['xug'], 
pjt=form.pjt.data.pjt, cse=form.cse.data.cse, rmk=form.rmk.data,
bw=session['xbw'], uw=session['xuw'], pw=session['xpw'], cw=session['xcw'], 
iow=session['xiow'], iog=session['xiog'], iwg=session['xiwg'])])

db.session.commit()

希望有人能帮助我,谢谢。

1 个答案:

答案 0 :(得分:1)

您可以将proc_id添加为任何其他字段。当您创建calc将其分配给variabl时,刷新会话并获取id(flush将获取要插入的行的id)。类似的东西:

calc = Calc(...)
db.session.add_all([calc])
db.session.flush()  # at this point calc has id
db.session.add(Matching(proc_id=calc.id, ...)
db.session.commit()