我有一个像这样的简单表
class Employeecode(Base):
__tablename__ = 'employeecode'
id = Column(Integer, primary_key=True)
unique_code = Column(String(5), unique=True)
employe_name = Column(String(50))
designation = Column(String(50))
如果'employeecode'
列没有该值,我如何创建一个将值插入'unique_code'
表的函数。
insert_unique(unique_code,employe_name,designation)
答案 0 :(得分:0)
首先,您可以将所有以前的unicode_value存储在列表中,然后您可以检查并运行查询..
def function(**kwargs):
session = Session()
unique_list =[]
employeecodes = db.session.query(Employeecode).all()
for employee in employeecodes:
unique_list.append(employee.unique_code)
if kwargs['unique_code'] not in unique_list:
employee = Employeecode()
employee.unique_code = kwargs['unique_code']
employee.employe_name = kwargs['employe_name']
employee.designation = kwargs['designation']
session.commit()
retval = row2dict(employee)
session.close()
return retval
else:
pass