def create(self, data):
        """Query helper for creating a row

        :param model: name of the table model
        :param fields: dict of columns:values to create
        """

        new_data = self._sanitize_for_create(data)

        if self.name in ('facts', 'attrs'):
            #checks that the node exists
            node = self.api._model_get_by_id('nodes', new_data['node_id'])

            query = 'node_id=%d and key="%s"' % (node['id'], new_data['key'])
            existing = self.api._model_query(self.name, query)
            if len(existing) != 0:
                return self.update(existing[0]['id'], data)

        r = self.model(**new_data)

        session.add(r)
        try:
            session.commit()
            return r.jsonify(api=self.api)
        except sqlalchemy.exc.IntegrityError as e:
            session.rollback()
            msg = "Unable to create %s, duplicate entry" % (self.name.title())
            raise exceptions.CreateError(msg)
        except sqlalchemy.exc.StatementError as e:
            session.rollback()
            # msg = e.message
            msg = "JSON object must be either type(dict) or type(list) " \
                  "not %s" % str(e)
            raise exceptions.CreateError(msg)
dataset/ETHPy150Open rcbops/opencenter/opencenter/db/abstraction.py/SqlAlchemyAbstraction.create/original