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.
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.
You don’t need to add anything directly to your association table, SQLAlchemy will do that. This is more or less from SQLAlchemy documentations: association_table = db.Table(‘association’, db.Model.metadata, db.Column(‘left_id’, db.Integer, db.ForeignKey(‘left.id’)), db.Column(‘right_id’, db.Integer, db.ForeignKey(‘right.id’)) ) class Parent(db.Model): __tablename__ = ‘left’ id = db.Column(db.Integer, primary_key=True) children = db.relationship(“Child”, secondary=association_table) class Child(db.Model): __tablename__ = ‘right’ id = db.Column(db.Integer, … Read more
If you are using Postgres < 9.4 you can’t update JSON field directly. You need flag_modified function to report the change to SQLAlchemy: from sqlalchemy.orm.attributes import flag_modified model.data[‘key’] = ‘New value’ flag_modified(model, “data”) session.add(model) session.commit()
You can define each User with a specific role. For example, user ‘x’ can be SCHOOL while user ‘y’ can be ‘STAFF’. class User(db.Model): __tablename__ = ‘User’ id = db.Column(db.Integer,primary_key=True) username = db.Column(db.String(80),unique=True) pwd_hash = db.Column(db.String(200)) email = db.Column(db.String(256),unique=True) is_active = db.Column(db.Boolean,default=False) urole = db.Column(db.String(80)) def __init__(self,username,pwd_hash,email,is_active,urole): self.username = username self.pwd_hash = pwd_hash self.email = … Read more
You will have to switch from using a plain, many-to-many relationship to using an “Association Object”, which is basically just taking the association table and giving it a proper class mapping. You’ll then define one-to-many relationships to User and Community: class Membership(db.Model): __tablename__ = ‘community_members’ id = db.Column(‘id’, db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey(‘user.id’)) community_id … Read more
You need to make sure that the value you pass to the directory argument is an absolute path, corrected for the current location of your application. The best way to do this is to configure UPLOAD_FOLDER as a relative path (no leading slash), then make it absolute by prepending current_app.root_path: @app.route(‘/uploads/<path:filename>’, methods=[‘GET’, ‘POST’]) def download(filename): … Read more
I’d say your question has nothing to do with flask at all. For example, you don’t have a problem with the templates, routes, views or logon decorators. Where you struggle at is at SQLAlchemy. So my suggestion is to ignore Flask for a while and get used to SQLAlchemy first. You need to get used … Read more
Retrieve an object using the tutorial shown in the Flask-SQLAlchemy documentation. Once you have the entity that you want to change, change the entity itself. Then, db.session.commit(). For example: admin = User.query.filter_by(username=”admin”).first() admin.email=”[email protected]” db.session.commit() user = User.query.get(5) user.name=”New Name” db.session.commit() Flask-SQLAlchemy is based on SQLAlchemy, so be sure to check out the SQLAlchemy Docs as … Read more
There are several ways to UPDATE using sqlalchemy 1) user.no_of_logins += 1 session.commit() 2) session.query(User).\ filter(User.username == form.username.data).\ update({‘no_of_logins’: User.no_of_logins + 1}) session.commit() 3) conn = engine.connect() stmt = User.update().\ values(no_of_logins=User.no_of_logins + 1).\ where(User.username == form.username.data) conn.execute(stmt) 4) setattr(user, ‘no_of_logins’, user.no_of_logins + 1) session.commit()