Re: [sqlalchemy] Cascade child updates onto the parent

2020-05-28 Thread Colton Allen
Perfect.  That's exactly what I ended up doing.  I added events 
(after_insert/update/delete) for each backref.

For each has-many relationship (through a secondary table) I had to 
consider the fact that the parent model would exist in session.dirty but 
not trigger the "onupdate" action on the column.  So I added a generic 
before_update/delete event on my models' base class which is basically just 
target.updated_at = dt.now().

On Thursday, May 28, 2020 at 11:06:28 AM UTC-5, Mike Bayer wrote:
>
>
>
> 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 sqlal...@googlegroups.com .
> To view this discussion on the web visit 
> https://groups.google.com/d/msgid/sqlalchemy/490bfcd2-4ebd-4df3-98a8-516caeacbd6a%40googlegroups.com
>  
> 
> .
>
>
>

-- 
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/555b8c57-85cb-48fa-9e29-491796877fd6%40googlegroups.com.


Re: [sqlalchemy] Cascade child updates onto the parent

2020-05-28 Thread Mike Bayer


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
>  
> .

-- 
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.


[sqlalchemy] Cascade child updates onto the parent

2020-05-27 Thread Colton Allen
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?

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.