1
votes

I'm getting an error after perform an insert statement.

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

Insert statement

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()

The message error

File "/Users/Ricardo/PycharmProjects/virtual_rfwire/lib/python3.3/site-packages/sqlalchemy/orm/loading.py", line 647, in load_scalar_attributes "contain a full primary key." % state_str(state)) sqlalchemy.exc.InvalidRequestError: Instance cannot be refreshed - it's not persistent and does not contain a full primary key.

This is happen behind scenes

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}
1
what is the result of func.UNHEX(args['guid_tag'])?Burhan Khalid
@BurhanKhalid I have updated the question.Ricardo
Your value is not a binary string.Burhan Khalid
@BurhanKhalid What do you mean?Ricardo

1 Answers

1
votes

I will post the good way to make an insert statement.

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()