1

I was trying to update a row in SQLite DB from flask app using flask-sqlalchemy. I trying to do the same with python 3.6.8. But the row is not getting updated and also there is no exception of any sort. The problem is happening with the code in the put method.

The Code

db = SQLAlchemy(app)

class MakeTemplate(Resource):


    def __init__(self):
        self.parser = reqparse.RequestParser()    

    def put(self):
        self.parser = reqparse.RequestParser()
        self.parser.add_argument('editProducte', type=str, required=True)
        self.parser.add_argument('editData', required=True)
        args = parser.parse_args()

        editProducte = args["editProduct"]
        new_json = args["editData"]

        model_json = models.Template.query.filter_by(
            product_name=editProducte).first()

        model_json.json_template = "{'new' :\" test\" }"
        model_json.update_date = datetime.datetime.utcnow()

        db.session.commit()


        return jsonify({"data": new_json})

I also tried this code which is mentioned in this stackoverflow Question

def put(self):

    parser.add_argument('editInvoice', type=str, required=True)
    parser.add_argument('editData', required=True)
    args = parser.parse_args()

    editInvoice = args["editInvoice"]
    new_json = args["editData"]



    rows_changed = models.Template.query.filter_by(
        invoice_name=editInvoice).update(dict(json_template={}))


    db.session.commit()

This the models.py where the model is defined

class Template(app.db.Model):
    id = app.db.Column(app.db.Integer, primary_key=True)
    created_date = app.db.Column(DATETIME, default=datetime.datetime.utcnow)
    update_date = app.db.Column(DATETIME)

    company = app.db.Column(VARCHAR)
    invoice_name = app.db.Column(VARCHAR, nullable=False, unique=True)

    image_location = app.db.Column(VARCHAR, nullable=True)
    json_template = app.db.Column(JSON, nullable=False)

    def __repr__(self):
        return '<User %r>' % self.company

Could someone please tell me the possible cause and solution? If I've missed out anything, over- or under-emphasized a specific point, let me know in the comments.

Nithin Varghese
  • 893
  • 1
  • 6
  • 28
  • 1
    Just to be sure, are the `db` in models.py and in your "the code" the same? It'd also help if you could strip all non essential code away from the example, or in other words produce a [mcve]. If you can make it so that others can copy, paste & run your example (and observe your results), the better. – Ilja Everilä Jun 24 '19 at 19:53
  • @IljaEverilä The DB is in the model. Will add the same in the code, Thanks – Nithin Varghese Jun 25 '19 at 04:19
  • Have you tried the alternate query style? `db.session.query(models.Template).filter_by…` – Nick K9 Jun 29 '19 at 06:09
  • Also, I’m not sure I understand what you mean by “the DB is in the model.” It’s declared in `app/__init__.py`, right? – Nick K9 Jun 29 '19 at 06:14

0 Answers0