SQLAlchemy Delete Persistency Error
<p>For some reason the code below is generating a persistency error:</p>

<pre><code>'&lt;Data at 0x1041db8d0&gt;' is not persisted
</code></pre>

<p>Does the object need to be initialized before it's deleted, what's wrong here?</p>

<pre><code>if request.method == 'POST' and form3.validate():
    data_entered = Data(notes=form3.dbDelete.data)
    try:
        db.session.delete(data_entered)
        db.session.commit()
        db.session.close()
        return render_template('deleted.html', notes=form3.dbDelete.data)
</code></pre>

<p>Much appreciated.</p>

<p><code>data_entered = Data(notes=form3.dbDelete.data)</code></p>

<p>This is creating a new instance (or row) of <code>Data</code>. Then you are running a <code>session.delete</code> procedure to remove it from the database, but it never was inserted (via <code>session.commit()</code> for example).</p>

<p>I'm guessing this is what you are trying to do:</p>

<pre><code>try:
    data_entered = db.session.query(Data).filter(Data.notes == form3.dbDelete.data).one()
except sqlalchemy.orm.exc.NoResultFound:
    pass
else:
    try:
        db.session.delete(data_entered)
        db.session.commit()
        db.session.close()
        return render_template('deleted.html', notes=form3.dbDelete.data)
</code></pre>

python sqlalchemy