On Wed, May 27, 2020, at 3:57 PM, Colton Allen wrote:
> Hello,
> 
> I'm trying to automate a backref update. Basically, when a child model is 
> inserted or updated I want the parent model's "updated_at" column to mutate. 
> The value should be the approximate time the user-child-model was updated. 
> The updated_at value would not have to match the created_at/updated_at value 
> on the child. It would just need to mutate to a new time.
> 
> class UserModel(db.Model):
>  updated_at = db.Column(db.DateTime, default=db.now, onupdate=datetime.now)
> 
> 
> class UserChildModel(db.Model):
>  user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
>  user = db.relationship('UserModel', backref='children')
> 
> user = UserModel()
> save(user)
> print(user.updated_at) # x
> 
> child = UserChildModel(user_id=user.id)
> save(child)
> print(user.updated_at) # y (value changed)
> 
> Hopefully this pseudocode is sufficient.
> 
> I'm wondering if there is an option I can specify on the orm.relationship 
> factory. Or will I need to define an event?

that could certainly be based on an event from the SQLAlchemy side. a very 
straightforward one would be the before_insert / before_update / after_insert / 
after_update suite of events, I would emit an UPDATE statement against the 
parent table using the foreign key on the child row that's being 
inserted/updated. Another approach would be a DB trigger.

the mapper level events are detailed at 
https://docs.sqlalchemy.org/en/13/orm/events.html?highlight=before_insert#sqlalchemy.orm.events.MapperEvents.before_insert

the "connection" right there is where you'd run your update, like:

connection.execute(update(parent).values(updated_at=datetime.now()).where(parent.id
 == inserted.parent_id))




> 
> Thanks!
> 

> --
>  SQLAlchemy - 
>  The Python SQL Toolkit and Object Relational Mapper
> 
> http://www.sqlalchemy.org/
> 
>  To post example code, please provide an MCVE: Minimal, Complete, and 
> Verifiable Example. See http://stackoverflow.com/help/mcve for a full 
> description.
>  --- 
>  You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
>  To unsubscribe from this group and stop receiving emails from it, send an 
> email to sqlalchemy+unsubscr...@googlegroups.com.
>  To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sqlalchemy/490bfcd2-4ebd-4df3-98a8-516caeacbd6a%40googlegroups.com
>  
> <https://groups.google.com/d/msgid/sqlalchemy/490bfcd2-4ebd-4df3-98a8-516caeacbd6a%40googlegroups.com?utm_medium=email&utm_source=footer>.

-- 
SQLAlchemy - 
The Python SQL Toolkit and Object Relational Mapper

http://www.sqlalchemy.org/

To post example code, please provide an MCVE: Minimal, Complete, and Verifiable 
Example.  See  http://stackoverflow.com/help/mcve for a full description.
--- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/sqlalchemy/57bd5e2e-f5e4-49de-9668-e48051364f17%40www.fastmail.com.

Reply via email to