执行插入语句后我收到错误。
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_rfwire/lib/python3.3/site-packages/sqlalchemy/orm/loading.py" ;, 第647行,在load_scalar_attributes中 "包含完整的主键。" %state_str(state))sqlalchemy.exc.InvalidRequestError:Instance 不能刷新 - 它不是持久的,不包含完整的 主键。
这是在幕后发生的
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}
答案 0 :(得分:1)
我将发布插入声明的好方法。
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()