Note that there are some explanatory texts on larger screens.

plurals
  1. POSQLAlchemy A conflicting state is already present in the identity map for key
    primarykey
    data
    text
    <p>I'm using the Flask-SqlAlchemy extension around SQLAlchemy to work with a SQLite database in Python.</p> <p>I have a many-to-many relationship set up between "Format" and "Movie" models. I have successfully set the relationship and performed queries with no problem. The problem occurs when I try to make an update to a movie's formats. Calling my update method results in a <code>AssertionError: A conflicting state is already present in the identity map for key (&lt;class 'moviecode.models.Format'&gt;, (1,))</code></p> <p>This error makes me think that the SQLAlchemy Session is not getting removed/cleaned up properly between the update calls/db queries, but adding a remove() call doesn't help.</p> <p><strong>mainapp.py</strong></p> <pre><code>app = Flask('moviecode') db = SQLAlchemy(app) </code></pre> <p><strong>views.py</strong></p> <pre><code>from updater import updateMovie @app.route("/admin/movies/&lt;int:movieId&gt;/refresh/",methods=['GET']) @login_required def refreshMovie(movieId): updateMovie(movieId) return redirect(url_for('admin')) </code></pre> <p><strong>updater.py</strong></p> <pre><code>from mainapp import db from models import Format, Movie def updateMovie(movieId): movie = Movie.query.filter_by(id=movieId).first() formats = Format.query.all() movie.formats = newFormats #ERROR IS THROWN HERE db.session.commit() db.session.remove() #Trying to clean up Session. Makes no difference. </code></pre> <p>Any help or insight would be much appreciated! I've tried lots of SqlAlchemy methods with no luck (expunging the platforms, merging the movie object, removing the session, etc). Also tried setting the movies formats to blank and committing the change to the db before adding back the new formats, but no change.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload