-3

i want to delete specific row in a db.table i have these code

num_rows_deleted=db.session.query(Orders).filter(Orders.c.id==id).first()
db.session.delete(num_rows_deleted)
db.session.commit()

Orders = db.Table('orders', db.Model.metadata,
            db.Column('id', db.Integer,primary_key=True),
              db.Column('user_id', db.Integer, db.ForeignKey('user.id')),
              db.Column('book_id', db.Integer, db.ForeignKey('books.id')),
              db.Column('quantity', db.Integer,default=1, nullable=False),
              db.Column('created_at',db.DateTime, default=datetime.datetime.now),
            db.Column('updated_at',db.DateTime, onupdate=datetime.datetime.now))

these is the table

but i get these error and i dont know how to fix it sqlalchemy.orm.exc.UnmappedInstanceError: Class 'sqlalchemy.util._collections.result' is not mapped

davidism
  • 121,510
  • 29
  • 395
  • 339
wael dagash
  • 1
  • 1
  • 2
  • Hi Wael, please show us the output as you get it in your python terminal, this will help people identify the issue. Also writing a toy problem that's shows your issue can help in resolving the issue faster. – sukhbinder Nov 05 '20 at 05:51
  • hi, these is the error that i get sqlalchemy.orm.exc.UnmappedInstanceError: Class 'sqlalchemy.util._collections.result' is not mapped – wael dagash Nov 05 '20 at 08:20
  • Does this answer your question? [How to delete a record by id in Flask-SQLAlchemy](https://stackoverflow.com/questions/27158573/how-to-delete-a-record-by-id-in-flask-sqlalchemy) – Ruben Helsloot Nov 05 '20 at 11:56

3 Answers3

0

Try the following instead, not fetching the value, but deleting it immediately, using Query.delete:

db.session.query(Orders).filter(Orders.c.id==id).delete()
db.session.commit()
Ruben Helsloot
  • 12,582
  • 6
  • 26
  • 49
  • thank you for ur answer , i get the same problem AttributeError: 'NoneType' object has no attribute 'class_' – wael dagash Nov 05 '20 at 08:18
  • What is the value of `id`? Is it a number or something else? – Ruben Helsloot Nov 05 '20 at 09:44
  • yes its a number - and itis the primary key of the order table. these the whole function from the views.py @app.route('/delete_order/',methods=["GET", "POST"]) @flask_login.login_required def delete_order(id): row_to_delete = db.session.query(Orders).filter(Orders.c.id == id).delete() db.session.delete(row_to_delete) db.session.commit() – wael dagash Nov 05 '20 at 11:29
  • Remove the `db.session.delete(row_to_delete)` part, just leave exactly what I wrote – Ruben Helsloot Nov 05 '20 at 11:45
  • 1
    still the same thing AttributeError: 'NoneType' object has no attribute 'class_' – wael dagash Nov 05 '20 at 11:54
0

You can try this

row_to_delete = db.session.query(Orders).filter(Orders.id==id).one()
db.session.delete(row_to_delete)
db.session.commit()
Mohammad Aarif
  • 1,619
  • 13
  • 19
0

thank you all - i used Deleting Rows from the Many to Many Table¶ from these site https://docs.sqlalchemy.org/en/13/orm/basic_relationships.html

my table is a seconday table and it is connected to two tables , so i used remove to delete , and append to add .

wael dagash
  • 1
  • 1
  • 2