I am trying to delete a record from my table. This is my code for the delete function.
@app.route("/delete_link/<link_id>", methods=['GET', 'POST']) def delete_link(link_id): link = models.Link.query.filter(models.Link.l_id == link_id).first() db.session.delete(link) db.session.commit() return flask.redirect(flask.url_for('links'))
line: db.session.delete(link) returns me this error:
InvalidRequestError: Object '' is already attached to the session '1' (this is '2')
I tried this code too:
@app.route("/delete_link/<link_id>", methods=['GET', 'POST']) def delete_link(link_id): link = models.Link.query.filter(models.Link.l_id == link_id) link.delete() db.session.commit() return flask.redirect(flask.url_for('links'))
which does not update the database. The link should not be in the session, I think, but I do not know how to check it and how to fix it. I am new to sqlalchemy.
EDIT
I use this to create a db variable that probably creates a session at this point (this is at the top of the code). It comes from flask documentation
from yourapplication import db
source share