Python SQLAlchemy - Update time stamp on parent model when child is updated - python

Ive created two models, Basket and BasketItem, shown below. A basket can have many basket items so I'm using a foreign key to create this relationship.
You can see that the Basket model has a updated_at field which holds a timestamp of when the basket was last updated. I would like this field to be updated when a BasketItem is added, removed or updated.
I've added an event listener but my solution doesn't seem very elegant. Is there a better way of doing this?
class Basket(Base):
__tablename__ = "baskets"
id = Column(String(45), primary_key=True, default=uuid4)
shop = Column(String(45), nullable=False)
currency_code = Column(String(3), nullable=False)
total = Column(String(15), nullable=False, default=0)
status = Column(Enum("open", "closed"), nullable=False, default="open")
created_at = Column(DateTime, default=datetime.datetime.utcnow)
updated_at = Column(DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.utcnow)
items = relationship("BasketItem", backref="basket")
class BasketItem(Base):
__tablename__ = "basket_items"
basket_id = Column(String(45), ForeignKey('baskets.id'), primary_key=True)
product_url = Column(String(90), nullable=False, primary_key=True)
quantity = Column(Integer, nullable=False)
#event.listens_for(BasketItem, 'after_insert')
#event.listens_for(BasketItem, 'after_update')
#event.listens_for(BasketItem, 'after_delete')
def basket_item_change(mapper, connection, target):
db_session = scoped_session(sessionmaker(autocommit=False, autoflush=False, bind=engine))
basket = db_session.query(Basket).get(target.basket_id)
basket.updated_at = datetime.datetime.utcnow()
db_session.commit()

I think it's good to use event listener but you can do something like below. When you add a new basketitem I am sure you are query parent first and then add this child with that parent. If yes then:
basket = Basket.query.get(id)
if basket:
basket.update()
# you can perform delete or update too
item = BasketItem(basket=basket, ...)
db.session.add(item)
db.session.commit()
class Basket(db.Model):
...
def update(self):
self.updated_at = datetime.utcnow()
db.session.add(self)
db.session.commit()

Related

Checks on foreign key of foreign key in SQLAlchemy

I have these 3 sql alchemy (sqla) models:
class Site(Base):
__tablename__ = "site"
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
name = Column(String, nullable=False)
created_at = Column(DateTime, default=datetime.utcnow())
updated_at = Column(DateTime, nullable=True, default=datetime.utcnow(), onupdate=datetime.utcnow)
class Camera(Base):
__tablename__ = "camera"
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
site_id = Column(UUID(as_uuid=True), ForeignKey("site.id"), nullable=False)
name = Column(String, nullable=False)
created_at = Column(DateTime, default=datetime.utcnow())
updated_at = Column(DateTime, nullable=True, default=datetime.utcnow(), onupdate=datetime.utcnow)
site = relationship("Site", backref="cameras")
class RtspServerEndpoint(Base):
__tablename__ = "rtsp_server_endpoint"
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
camera_id = Column(UUID(as_uuid=True), ForeignKey("camera.id"), nullable=False)
rtsp_url_endpoint = Column(String, nullable=False)
rtsp_username = Column(String, nullable=False)
rtsp_encrypted_password = Column(String, nullable=False)
name = Column(String, nullable=False)
created_at = Column(DateTime, default=datetime.utcnow())
updated_at = Column(DateTime, nullable=True, default=datetime.utcnow(), onupdate=datetime.utcnow)
camera = relationship("Camera", backref="rtsp_server_endpoint", lazy="joined")
camera_id is the foreign key of rtspserverendpoint table and site_id is the foreign key for the Camera table.
When a user wants to add a new rtspserverendpoint record, he makes an HTTP request such as:
POST sites/<site_id>/camera/<camera_id>/rtspserverendpoint
Before adding the new rtspserverendpoint, I would like to make sure that the <site_id> and the <camera_id> are consistent, as a security. I can probably make a separate query just to check that, such as:
check_record_exist = db.session.query(Camera).filter(Camera.site_id == site_id).first()
if not check_record_exist:
raise ("No such camera with this site_id")
But what I would like to know, is if there is a more elegant way to check that: For example, adding a constraint in my Base models that would forbid adding such an inconsistent record in the database.
I am not aware of any straightforward way to implement this 2-level check on the database directly.
In fact, the only consistency that the database should know about is that your new RtspServerEndpoint instance will belong to the correct Camera instance. But this will be correct by default by the way you will be creating the RtspServerEndpoint instance.
Therefore, in my opinion, the check of the correctness of the site_id in the URL of the POST request should be implemented in the logic of your code. I would probably do it along these lines:
#handler(..., method='POST')
def rtspserverendpoint(site_id: int, camera_id: int):
# find camera, which will allow us to check the correctness of the site_id as well
camera = db.session.query(Camera).get(camera_id)
if camera is None:
raise Exception(f"No camera with this {camera_id=}.")
if camera.site_id != site_id:
raise Exception(f"Camera with this {camera_id=} does not belong to the site with {site_id=}.")
new_obj = RtspServerEndpoint(
...,
camera_id=camera_id,
...,
)
db.session.add(new_obj)
db.session.commit()

flask-sqlalchemy, one-many relationship, foreign key changes into NULL

I have user table and album table etc.. and user-album have one-many relationship.
But when a user associates with one or more albums, the foreign key excluding the latest one from the album table changes null. This is the case that user_uid=1 have 3 albums and user_uid=2 have 1 album.(BUT foreign key having user_uid=1 is only just one. And this problem also occurs everywhere having one-many relationship. Here is my code..
class User(Base):
__tablename__ = 'user'
uid = Column(Integer, primary_key=True)
username = Column(String(10), unique=True, nullable=False)
email = Column(String(35), unique=True, nullable=False)
salted_password = Column(String(100), unique=True, nullable=False)
profile_pic = Column(String(100))
authorization = Column(Boolean)
expiry = Column(DATETIME)
fcm_token = Column(String(45))
created_at = Column(DATETIME)
albums = relationship('Album')
notifications = relationship('Notification')
like_photo = relationship('Photo', secondary=like_photo)
follow_album = relationship('Album', secondary=follow_album)
followed = relationship('User',
secondary=followers,
primaryjoin=(followers.c.follower_id == uid),
secondaryjoin=(followers.c.followed_id == uid),
backref=backref('followers', lazy='dynamic'),
lazy='dynamic')
comment_photo = relationship('Photo', secondary=comment)
class Album(Base):
__tablename__ = 'album'
aid = Column(Integer, primary_key=True)
title = Column(String(45), nullable=False)
created_at = Column(DATETIME)
user_uid = Column(Integer, ForeignKey('user.uid'))
photos = relationship('Photo')
album_tags = relationship('Album_tag')
And I updated album table like below..
u = User.query.filter(User.uid == session['uid']).first()
u.albums = [Album(title=request.json['title'], created_at=datetime.utcnow())]
db_session.add(u)
db_session.commit()
I don't know why..
I believe you need to do it the other way around, since in your way, you are overriding user's albums list:
coffee_album = Album(title=request.json['title'], \
created_at=datetime.utcnow())
u = User.query.filter(User.uid == session['uid']).first()
coffe_album.user_uid = u.uid
db_session.add(coffee_album)
db_session.commit()

How to obtain data from a table that has been joined

I have two tables items and games.
#app.route('/collection/<username>/<int:page>/<platform>/<path:path>')
def collection(username, page=1, platform='DMG', path=None):
# first I get the user by his username
user = User.query.filter_by(username=username).first()
# then I get all items form the user and related games
items = user.items.join(Game)
# until now it works perfectly fine
# now I would like to obtain all titles from the joined table games
game_titles = items.filter(Game.title).all()
# but unfortunately I get only an empty list
What is missing?
Here my models:
class Game(db.Model):
__tablename__ = 'games'
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(64), index=True)
publisher = db.Column(db.String(32), index=True)
region = db.Column(db.String(3), index=True)
code_platform = db.Column(db.String(3), index=True)
code_identifier = db.Column(db.String(4), index=True)
code_region = db.Column(db.String(3), index=True)
code_revision = db.Column(db.String(1))
code = db.Column(db.String(16), index=True, unique=True)
year = db.Column(db.Integer)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
items = db.relationship('Item', backref='game', lazy='dynamic')
def __repr__(self):
return '<Game %r>' % (self.title)
class Item(db.Model):
__tablename__ = 'items'
id = db.Column(db.Integer, primary_key=True)
code = db.Column(db.String(8), index=True)
cart = db.Column(db.Boolean)
box = db.Column(db.Boolean)
manual = db.Column(db.Boolean)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
game_id = db.Column(db.Integer, db.ForeignKey('game.id'))
def __repr__(self):
return '<Collection %r>' % (self.user_id)
You have two options. Using SQLAlchemy ORM:
game_titles = [i.game.title for i in user.items]
To make this more efficient, you can apply the joinedload optimization:
game_titles = [i.game.title for i in user.items.options(joinedload(Item.game))]
Alternatively, you can use SQLAlchemy core if all you care about are the titles (and nothing else):
game_titles = user.items.join(Item.game).with_entities(Game.title).all()
You can even skip fetching the user altogether if you don't care about the user at all:
game_titles = User.query.join(User.items).join(Item.game).filter(User.username == username).with_entities(Game.title).all()
As an aside, .filter and .filter_by correspond to the selection operator in relational algebra, whereas .with_entities and db.session.query(...) correspond to the projection operator, contrary to what you had initially assumed.
Try something like this:
items.join(Game).options(joinedload(Item.game, innerjoin=True))
Essentially, you're joining with Game and explicitly loading it, where the innerjoin forces it to do so only on the games listed in the table you're joining with (items)

Creating new model in sqlalchemy triggers INSERT on database

While writing new test I found quite curious case:
models.ArticleTag(tag=tag, article=article)
Triggers INSERT statement on database even though there is no Session.add after.
However if I change code to look like this:
models.ArticleTag(tag.id=tag.id, article=article)
No INSERT will ever happen.
Article in this case is fresh object that is not in db yet and tag is something that we have already in db.
My models look like this:
class Tag(object):
__tablename__ = 'tag'
title = sqlalchemy.Column(sqlalchemy.Unicode(255), nullable=False)
type = sqlalchemy.Column(sqlalchemy.Unicode(255))
uid = sqlalchemy.Column(sqlalchemy.Unicode(32), primary_key=True, nullable=False)
__table_args__ = (
sqlalchemy.UniqueConstraint('type', 'title', name='title_type_unique'),
)
class ArticleTag(object):
"""Represent m2m table between Tags and Article.
association obj for mapping m2m articles<->tags
"""
__tablename__ = 'article_tag'
article_uid = sqlalchemy.Column(sqlalchemy.Unicode(32), sqlalchemy.ForeignKey('article.uid'), primary_key=True)
tag_uid = sqlalchemy.Column(sqlalchemy.Unicode(32), sqlalchemy.ForeignKey('tag.uid'), primary_key=True)
priority = sqlalchemy.Column(sqlalchemy.Integer, default=0)
tag = sqlalchemy.orm.relationship('Tag', backref='articles')
class Article(object):
__tablename__ = 'article'
uid = sqlalchemy.Column(sqlalchemy.Unicode(32), primary_key=True, nullable=False)
title = sqlalchemy.Column(sqlalchemy.Unicode(255), nullable=False)
meta_description = sqlalchemy.Column(sqlalchemy.Unicode(255))
meta_title = sqlalchemy.Column(sqlalchemy.Unicode(255))
body = sqlalchemy.Column(sqlalchemy.Unicode)
is_active = sqlalchemy.Column(sqlalchemy.Boolean, default=True)
# any tags we have
article_tags = sqlalchemy.orm.relationship('ArticleTag', backref='article', cascade='all, delete-orphan', order_by=lambda: sqlalchemy.desc(ArticleTag.priority))
#property
def tags(self):
"""Helper method that will return tags sorted by priority.
"""
return [art_tag.tag for art_tag in self.article_tags]
My guess is that I do not know something quite important about sqlalchemy.
Anyone can point me a direction?

How to set one to many and one to one relationship at same time in Flask-SQLAlchemy?

I'm trying to create one-to-one and one-to-many relationship at the same time in Flask-SQLAlchemy. I want to achieve this:
"A group has many members and one administrator."
Here is what I did:
class Group(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(140), index=True, unique=True)
description = db.Column(db.Text)
created_at = db.Column(db.DateTime, server_default=db.func.now())
members = db.relationship('User', backref='group')
admin = db.relationship('User', backref='admin_group', uselist=False)
def __repr__(self):
return '<Group %r>' % (self.name)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
group_id = db.Column(db.Integer, db.ForeignKey('group.id'))
admin_group_id = db.Column(db.Integer, db.ForeignKey('group.id'))
created_at = db.Column(db.DateTime, server_default=db.func.now())
However I got an error:
sqlalchemy.exc.AmbiguousForeignKeysError: Could not determine join
condition between parent/child tables on relationship Group.members -
there are multiple foreign key paths linking the tables. Specify the
'foreign_keys' argument, providing a list of those columns which
should be counted as containing a foreign key reference to the parent
table.
Does anyone know how to do that properly?
The solution is to specify the foreign_keys argument on all relationships:
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
group_id = Column(Integer, ForeignKey('groups.id'))
admin_group_id = Column(Integer, ForeignKey('groups.id'))
class Group(Base):
__tablename__ = 'groups'
id = Column(Integer, primary_key=True)
members = relationship('User', backref='group', foreign_keys=[User.group_id])
admin = relationship('User', backref='admin_group', uselist=False, foreign_keys=[User.admin_group_id])
Perhaps consider the admin relation in the other direction to implement "a group has many members and one admin":
class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
group_id = Column(Integer, ForeignKey('groups.id'))
group = relationship('Group', foreign_keys=[group_id], back_populates='members')
class Group(Base):
__tablename__ = 'groups'
id = Column(Integer, primary_key=True)
members = relationship('User', foreign_keys=[User.group_id], back_populates='group')
admin_user_id = Column(Integer, ForeignKey('users.id'))
admin = relationship('User', foreign_keys=[admin_user_id], post_update=True)
See note on post_update in the documentation. It is necessary when two models are mutually dependent, referencing each other.
The problem you're getting comes from the fact that you've defined two links between your classes - a User has a group_id (which is a Foreign Key), and a Group has an admin (which is also defined by a Foreign Key). If you remove the Foreign Key from the admin field the connection is no longer ambiguous and the relationship works. This is my solution to your problem (making the link one-to-one):
from app import db,app
class Group(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(140), index=True, unique=True)
description = db.Column(db.Text)
created_at = db.Column(db.DateTime, server_default=db.func.now())
admin_id = db.Column(db.Integer) #, db.ForeignKey('user.id'))
members = db.relationship('User', backref='group')
def admin(self):
return User.query.filter_by(id=self.admin_id).first()
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), unique=True)
group_id = db.Column(db.Integer, db.ForeignKey('group.id'))
created_at = db.Column(db.DateTime, server_default=db.func.now())
The one drawback to this is that the group object doesn't have a neat admin member object you can just use - you have to call the function group.admin() to retrieve the administrator. However, the group can have many members, but only one of them can be the administrator. Obviously there is no DB-level checking to ensure that the administrator is actually a member of the group, but you could add that check into a setter function - perhaps something like:
# setter method
def admin(self, user):
if user.group_id == self.id:
self.admin_id = user.id
# getter method
def admin(self):
return User.query.filter_by(id=self.admin_id).first()
Ok, I found a workaround for this problem finally. The many-to-many relationship can coexist with one-to-many relationship between the same two tables at the same time.
Here is the code:
groups_admins = db.Table('groups_admins',
db.Column('user_id', db.Integer, db.ForeignKey('user.id')),
db.Column('group_id', db.Integer, db.ForeignKey('group.id'))
)
class Group(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(140), index=True, unique=True)
description = db.Column(db.Text)
created_at = db.Column(db.DateTime, server_default=db.func.now())
members = db.relationship('User', backref='group')
admins = db.relationship('User',
secondary=groups_admins,
backref=db.backref('mod_groups', lazy='dynamic'),
lazy='dynamic')
def __repr__(self):
return '<Group %r>' % (self.name)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
group_id = db.Column(db.Integer, db.ForeignKey('group.id'))
created_at = db.Column(db.DateTime, server_default=db.func.now())
I still want someone to tell me how to set one-to-many and one-to-one relationship at the same time, so I leave my answer here and won't accept it forever.
This link solved it for me
most important thing is to specify foreign_keys value in the relation as well as the primary join

Categories

Resources