How to Delete a Record by Id in Flask-Sqlalchemy

How to delete a record by id in Flask-SQLAlchemy

You can do this,

User.query.filter_by(id=123).delete()

or

User.query.filter(User.id == 123).delete()

Make sure to commit for delete() to take effect.

how to delete a specific row from a table sqlalchemy with flask?

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 .

How to delete a record from a database in flask-SQLAlchemy?

You are trying to delete a new instance of user. Which you created with

usr = users(user, email)

Since user, email is not the primary key, this is a different user instance than the user you're trying to delete. You should delete found_user.
eg:

if found_user:
db.session.delete(found_user)
db.session.commit()
flash("An Account has been Deleted!")

Delete individual SQLAlchemy row from an HTML table with flask

Implement a route to handle the deletion:

def merch_delete(mid):
merch = Merchant.query.filter_by(id=mid).first()
if merch:
msg_text = 'Merchant %s successfully removed' % str(merch)
cs.delete(merch)
cs.commit()
flash(msg_text)
return redirect(url_for('merchants_view'))

Then, add the following to a column in your jinja table:

<a href="{{ url_for('merch_delete', mid=single_merchant.id) }}"
onclick="return confirm('Do you want to permanently delete merchant {{ merchant }}?');" title="Delete Merchant">
<i class="material-icons" style="font-size:16px">delete</i></a>

The icon and Js verification step are optional.

Delete one row from database with SqlAlchemy Flask?

If you're using flask-sqlalchemy you can simplify a lot of this to leverage the power of sqlalchemy. FWIW you shouldn't be accessing the engine directly like this unless you have a more advanced use case you should use the session

E.g. You have mapped your data model like this:

class Subscriber(db.Model):
user_id = db.Column(db.Integer, primary_key=true)
client_id = ....
....

# method to add new subscriber
def __init__(self, user_id, client_id)
self.user_id = user_id
self.client_id = client_id

@app.route('/subscribe/')
def subscribe():
# add a subscriber with user id 21, client 543
new = Subscriber(21, 543)
db.session.add(new)
db.session.commit()

@app.route('/unsubscribe/')
def unsubscribe():
# remove subscriber
Subscriber.query.filter_by(user_id=21, client_id=543).delete()
db.session.commit()

How to remove 1 record from a db.Table using Flask_SQLAlchemy?

Given the a User instance and the Book instance to be deleted from the User's books, the Book can be removed like this:

user_instance.books.remove(book_instance)
db.session.commit()

So the remove function would look like this:

def remove(book_id):
# get the user
user = db.session.query(User).filter_by(email=session['email']).first()

# Find the book by its id
book_rm = Book.query.get(book_id)

user.books.remove(book_rm)
db.session.commit()

See the SQLAlchemy docs for more information.

Flask SQLAlchemy bulk deleting records

Skilljoin.query.filter_by(staffid=30).all() returns a list of the result of the query.

To delete either use:

skilljoins = Skilljoin.query.filter_by(staffid=30).all()
for skilljoin in skilljoins :
db.session.delete(skilljoin)
db.session.commit()

or

Skilljoin.query.filter_by(staffid=30).delete() 

(https://docs.sqlalchemy.org/en/13/orm/query.html#sqlalchemy.orm.query.Query.delete)



Related Topics



Leave a reply



Submit