0

When I post a new Device element, I'm not able to add the related reports

curl -X POST -H "Content-Type: application/json"  -d '{
  "reports": [1],
  "short_description": "test1",
  "code": "TEST"
}' "http://localhost:4000/devices"

I get the following error (server throws a 500 error). Any ideas?

Database is sqlite, but I don't think it is related

Thanks

AttributeError: 'int' object has no attribute '_sa_instance_state'

SQL Alchemy conf

engine = create_engine(config.DATABASE_URI, echo=True)
session = scoped_session(sessionmaker(autocommit=False,
                                      autoflush=False,
                                      bind=engine))
Base = declarative_base()


ReportDevice = Table(
    'report_devices',
    Base.metadata,
    Column('report_id', Integer, ForeignKey('reports._id')),
    Column('device_id', Integer, ForeignKey('devices._id'))
)



class Report(CommonColumns):
    __tablename__ = 'reports'

    _id = Column(Integer, primary_key=True)
    short_description = Column(String)
    code = Column(String)
    description = Column(String)
    status = Column(String)
    stored_procedure_id = Column(Integer, ForeignKey('stored_procedures._id'))
    stored_procedure = relationship('StoredProcedure')
    template_file = Column(String)
    _fields = Column(String)

    def __init__(self, _id, short_description, code, description,
                 status, template_file, fields, stored_procedure):
        self._id = _id
        self.short_description = short_description
        self.code = code
        self.description = description
        self.status = status
        self.stored_procedure = stored_procedure
        self.template_file = template_file
        self.fields = fields

    @hybrid_property
    def fields(self):
        return self._fields.split(';')

    @fields.setter
    def set_fields(self, fields):
        self._fields = ';'.join(fields)

    def render(self):
        env = Environment(loader=FileSystemLoader(config.TEMPLATES_DIR))
        template = env.get_template(self.template_file)
        data = self.store_procedure.execute(self.fields)
        return template.render({'rows': data})

......


class Device(CommonColumns):
    __tablename__ = 'devices'

    _id = Column(Integer, primary_key=True)
    short_description = Column(String)
    code = Column(String)
    description = Column(String)
    location = Column(String)
    status = Column(String)
    reports = relationship('Report', secondary=ReportDevice, backref='Device')
    rotation_interval = Column(Integer)
    advertisement = Column(String)

Eve

registerSchema('device')(Device)
registerSchema('report')(Report)



settings = {
    'DEBUG': True,
    'IF_MATCH': False,
    'ENFORCE_IF_MATCH': False,
    'SQLALCHEMY_DATABASE_URI': config.DATABASE_URI,
    'DOMAIN': {
        'devices': Device._eve_schema['device'],
        'reports': Report._eve_schema['report']
    }
}

settings['DOMAIN']['devices'].update({
    'resource_methods': ['GET', 'POST', 'DELETE'],
    'item_methods': ['GET', 'PATCH', 'DELETE']
})

app = Eve(validator=ValidatorSQL, data=SQL, settings=settings)
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Gaston Pisacco
  • 213
  • 1
  • 3
  • 10
  • Based on http://stackoverflow.com/questions/16151729/attributeerror-int-object-has-no-attribute-sa-instance-state it sounds like an INSERT is being attempted with an object being directed to the integer field (perhaps _id_) – Sᴀᴍ Onᴇᴌᴀ Dec 04 '16 at 06:35
  • I've seen that post, but is not the same issue as in my case the conversion is handled by eve-sqlalchemy plugin inside the python-eve framework. – Gaston Pisacco Dec 05 '16 at 13:50

0 Answers0