执行insert语句后,我收到一个错误。
class Tag(db.Model):
__tablename__ = "tags"
guid_tag = db.Column(db.BINARY(16), primary_key=True)
id_project = db.Column(db.Integer,db.ForeignKey("projects.id_project"))
project = db.relationship(Proyecto, backref=db.backref('list_tags', lazy='dynamic'))
type = db.Column(db.Integer) #(0,hub);(1,cable);(2,pipe);(3,electrical_pipes)
created_at = db.Column(db.DateTime)
updated_at = db.Column(db.DateTime)
def __init__(self,guid_tag,project,type,created_at):
self.guid_tag = guid_tag
self.project = project
self.type = type
self.created_at = created_at
self.updated_at = created_at
插入语句
guid_tag = func.UNHEX(args['guid_tag'])
nuevo_tag = Tag(guid_tag,id_project,type,creado_en)
db.session.add(nuevo_tag)
db.session.commit()
消息错误
文件“/Users/Ricardo/PycharmProjects/virtual\u rfwire/lib/python3.3/site packages/sqlalchemy/orm/loading.py”,第647行,在load\u scalar\u attributes中“包含完整的主键”。%国家炼金术。exc.InvalidRequestError:无法刷新实例-它不是持久的,并且不包含完整的主键。
这是在幕后发生的
2016-03-22 05:56:57,838 INFO sqlalchemy.engine.base.Engine INSERT INTO tags (guid_tag, id_project, type, created_at, updated_at) VALUES (UNHEX(%(UNHEX_1)s), %(id_project)s, %(type)s, %(created_at)s, %(updated_at)s)
2016-03-22 05:56:57,838 INFO sqlalchemy.engine.base.Engine {'created_at': datetime.datetime(2016, 3, 22, 4, 56, 57, 836091), 'UNHEX_1': '110E8400E29B11D4A716446655440000', 'id_project': 1, 'updated_at': datetime.datetime(2016, 3, 22, 4, 56, 57, 836091), 'type': 0}
我将发布制作insert语句的好方法。
import binascii
guid_tag = binascii.unhexlify(args['guid_tag'])
nuevo_tag = Tag(guid_tag,id_project,type,creado_en)
db.session.add(nuevo_tag)
db.session.commit()