I'm trying to do the following: loop through a series of records, if one already exists, I update it. If it doesn't exist, I add it.
For some reason, the adding is working fine, but the updating is not occurring. here's the code:
for interchange in data:
ltype='asdf'
plink='qwer'
e = Part.query.filter_by(interchange=interchange, light_type=ltype).first()
if e is None:
notpresent = notpresent + 1
p = Part(interchange=interchange,light_type=ltype,partslink=plink,ptype=ptype)
db.session.add(p)
else:
present = present + 1
e.partslink = plink
e.ptype = ptype
db.session.commit()
I print out the present and notpresent variables and both are > 0.
Am I handling the update part incorrectly when the record is found? That portion is not getting saved.