[sqlalchemy] after_commit and failure

2017-09-18 Thread cecemel
Hi, 

will the after_commit event get fired if the commit failed?

Don't see anything in the doc about that. 

thx

-- 
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 post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] Re: getting model's grandparent after persistent_to_deleted event has fired.

2017-08-22 Thread cecemel
Hello, 

thanks again for the answer. Perhaps my case was not clear enough. To make 
it a bit more explicit, I updated the example accordingly. 

from sqlalchemy import event

from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, backref, relationship

Base = declarative_base()


#
# MODEL
#
class House(Base):
__tablename__ = 'house'
id = Column(Integer, primary_key=True)
rooms = relationship("Room",
 backref=backref("house", lazy="joined"),
 cascade='all, delete-orphan')


class Room(Base):
__tablename__ = 'room'
id = Column(Integer, primary_key=True)
house_id = Column(Integer, ForeignKey('house.id'))
beds = relationship("Bed",
backref=backref("room", lazy="joined"),
cascade='all, delete-orphan')


class Bed(Base):
__tablename__ = 'bed'
id = Column(Integer, primary_key=True)
room_id = Column(Integer, ForeignKey('room.id'))


#
# CONFIG
#
def setup():
engine = create_engine("sqlite://", echo=True)

Base.metadata.bind = engine
Base.metadata.create_all(engine)

SessionFactory = sessionmaker(
bind=engine
)

event.listen(SessionFactory, 'deleted_to_detached', 
listener_bed_has_been_removed)

return SessionFactory


def listener_bed_has_been_removed(session, instance):
if type(instance) is not Bed:
return

# so, in this example, this function should be called 3 times.
# The first time it is called, I get no problems, I can access 
instance.room.house_id the call proceeds
# The second bed is a problem, I get the error
# "sqlalchemy.orm.exc.DetachedInstanceError: Parent instance  is not bound to a Session; lazy load operation of attribute 
'room' cannot proceed"

# SO, my question is: is there ANY way to keep these references to parents 
in this function?

bed_id = instance.id
house_id = instance.room.house_id


print("execute a service call to external service here bed_id {}, house_id 
{}".format(bed_id, house_id))


if __name__ == "__main__":
session_factory = setup()
session = session_factory()

session.add(House(id=1))
session.add(Room(id=1, house_id=1))
session.add(Bed(id=1, room_id=1))
session.add(Bed(id=2, room_id=1))
session.add(Bed(id=3, room_id=1))
session.commit()

room = session.query(Room).get(1)
session.delete(room)
session.commit()
session.close()


So, for this example, I am looking for a solution to keep the references to 
the 'house' from the 'bed' model after flush (any solution would be good). 
Is there perhaps a way to dynamically set a property in 'bed' -model (e.g 
house_id), once 'room'-backref has been loaded ?


On Tuesday, August 22, 2017 at 5:36:39 PM UTC+2, Mike Bayer wrote:
>
> you would need to illustrate an MCVE of what's happening.  objects 
> don't "lose track" of their related objects unless yes, you deleted 
> them, in which case you should not expect that they would be there. 
> when the object is expired, it will go to reload them, and they'll be 
> gone. 
>
> On Tue, Aug 22, 2017 at 11:05 AM, cecemel <ruiz.fe...@gmail.com 
> > wrote: 
> > @update: 
> > 
> > calling the flush doen't seem to make any difference. At some point, the 
> > object looses track of it's grandparents 
> > 
> > 
> > On Tuesday, August 22, 2017 at 3:57:23 PM UTC+2, cecemel wrote: 
> >> 
> >> Hi, 
> >> 
> >> so, I'm currently facing this issue, where I would like to do some 
> calls 
> >> to an external service, when my object has been deleted within a flush. 
> >> For this operation to occur, I need the id from my model object, but 
> also 
> >> the id from the parent of the parent object model. There are cases, 
> where I 
> >> am unable to access them (I guess, depending of the order of the 
> execution) 
> >> and I am unsure on what to do next. 
> >> 
> >> So if you're willing to give me some advice, would be awesome. 
> >> 
> >> Here is a dummy model: 
> >> 
> >> from sqlalchemy import event 
> >> 
> >> from sqlalchemy import * 
> >> from sqlalchemy.ext.declarative import declarative_base 
> >> from sqlalchemy.orm import sessionmaker, backref, relationship 
>

[sqlalchemy] Re: getting model's grandparent after persistent_to_deleted event has fired.

2017-08-22 Thread cecemel
@update: 

calling the flush doen't seem to make any difference. At some point, the 
object looses track of it's grandparents

On Tuesday, August 22, 2017 at 3:57:23 PM UTC+2, cecemel wrote:
>
> Hi, 
>
> so, I'm currently facing this issue, where I would like to do some calls 
> to an external service, when my object has been deleted within a flush. 
> For this operation to occur, I need the id from my model object, but also 
> the id from the parent of the parent object model. There are cases, where I 
> am unable to access them (I guess, depending of the order of the execution) 
> and I am unsure on what to do next.
>
> So if you're willing to give me some advice, would be awesome. 
>
> Here is a dummy model:
>
> from sqlalchemy import event
>
> from sqlalchemy import *
> from sqlalchemy.ext.declarative import declarative_base
> from sqlalchemy.orm import sessionmaker, backref, relationship
>
> Base = declarative_base()
>
>
> #
> # MODEL
> #
> class House(Base):
> __tablename__ = 'house'
> id = Column(Integer, primary_key=True)
> rooms = relationship("Room",
>  backref=backref("house", lazy="joined"),
>  cascade='all, delete-orphan')
>
>
> class Room(Base):
> __tablename__ = 'room'
> id = Column(Integer, primary_key=True)
> house_id = Column(Integer, ForeignKey('house.id'))
> beds = relationship("Bed",
> backref=backref("room", lazy="joined"),
> cascade='all, delete-orphan')
>
>
> class Bed(Base):
> __tablename__ = 'bed'
> id = Column(Integer, primary_key=True)
> room_id = Column(Integer, ForeignKey('room.id'))
>
>
> #
> # CONFIG
> #
> def setup():
> engine = create_engine("sqlite:///foo.db", echo=True)
>
> Base.metadata.bind = engine
> Base.metadata.create_all(engine)
>
> SessionFactory = sessionmaker(
> bind=engine
> )
>
> event.listen(SessionFactory, 'deleted_to_detached', 
> listener_bed_has_been_removed)
>
> return SessionFactory
>
>
> def listener_bed_has_been_removed(session, instance):
> if type(instance) is not Bed:
> return
>
> bed_id = instance.id
> house_id = instance.room.house.id  # this is NOT ALWAYS there. Depending 
> on the order of the execution I guess
>
> print("execute the service call to external service here bed_id {}, 
> house_id {}".format(bed_id, house_id))
> 
>
>  
>
> So my question(s):
>
>- Is there a clean way to always acces this parent's parent attribute?
>   - If not, would be starting a new session and query it from the 
>   event handler be an option? (is it not dangerous, because it seems to 
> work)
>  - Additional quirk, I am working within a transaction manager 
>  (pyramid_tm) and ZopeTransactionExtension()
>   
> Thanks!
>
>
> More information about the system:
>
> SQLAlchemy 1.1.13
>
> Python 3.5
>
> Postgres 9.6
>

-- 
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 post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


Re: [sqlalchemy] getting model's grandparent after persistent_to_deleted event has fired.

2017-08-22 Thread cecemel
Hi, 

thanks for your reply.

 I am confused, it is during a delete that I should access the grandparent 
id of my object. (would if have been a add, I wouldn't be confused)

 If I do a flush, I actually would expect the ID's to be removed and the 
object to be completely 'unbound'. (So no linked Ids to Room and House, 
since the row does not exist anymore)

Could you elaborate a bit, or point to some documentation?

thanks!

On Tuesday, August 22, 2017 at 4:33:08 PM UTC+2, Mike Bayer wrote:
>
> On Tue, Aug 22, 2017 at 9:57 AM, cecemel <ruiz.fe...@gmail.com 
> > wrote: 
> > Hi, 
> > 
> > so, I'm currently facing this issue, where I would like to do some calls 
> to 
> > an external service, when my object has been deleted within a flush. 
> > For this operation to occur, I need the id from my model object, but 
> also 
> > the id from the parent of the parent object model. There are cases, 
> where I 
> > am unable to access them (I guess, depending of the order of the 
> execution) 
> > and I am unsure on what to do next. 
>
> if you need auto-generated primary key values to be present, use 
> session.flush() to ensure it's happened. 
>
> > So my question(s): 
> > 
> > Is there a clean way to always acces this parent's parent attribute? 
> > 
> > If not, would be starting a new session and query it from the event 
> handler 
> > be an option? (is it not dangerous, because it seems to work) 
> > 
> > Additional quirk, I am working within a transaction manager (pyramid_tm) 
> and 
> > ZopeTransactionExtension() 
>
> assuming things are flushed you should be able to access the object 
> graph and the various .id attributes fully.   session.flush() will get 
> you there. 
>
> > 
> > Thanks! 
> > 
> > 
> > More information about the system: 
> > 
> > SQLAlchemy 1.1.13 
> > 
> > Python 3.5 
> > 
> > Postgres 9.6 
> > 
> > -- 
> > 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+...@googlegroups.com . 
> > To post to this group, send email to sqlal...@googlegroups.com 
> . 
> > Visit this group at https://groups.google.com/group/sqlalchemy. 
> > For more options, visit https://groups.google.com/d/optout. 
>

-- 
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 post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] getting model's grandparent after persistent_to_deleted event has fired.

2017-08-22 Thread cecemel
Hi, 

so, I'm currently facing this issue, where I would like to do some calls to 
an external service, when my object has been deleted within a flush. 
For this operation to occur, I need the id from my model object, but also 
the id from the parent of the parent object model. There are cases, where I 
am unable to access them (I guess, depending of the order of the execution) 
and I am unsure on what to do next.

So if you're willing to give me some advice, would be awesome. 

Here is a dummy model:

from sqlalchemy import event

from sqlalchemy import *
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, backref, relationship

Base = declarative_base()


#
# MODEL
#
class House(Base):
__tablename__ = 'house'
id = Column(Integer, primary_key=True)
rooms = relationship("Room",
 backref=backref("house", lazy="joined"),
 cascade='all, delete-orphan')


class Room(Base):
__tablename__ = 'room'
id = Column(Integer, primary_key=True)
house_id = Column(Integer, ForeignKey('house.id'))
beds = relationship("Bed",
backref=backref("room", lazy="joined"),
cascade='all, delete-orphan')


class Bed(Base):
__tablename__ = 'bed'
id = Column(Integer, primary_key=True)
room_id = Column(Integer, ForeignKey('room.id'))


#
# CONFIG
#
def setup():
engine = create_engine("sqlite:///foo.db", echo=True)

Base.metadata.bind = engine
Base.metadata.create_all(engine)

SessionFactory = sessionmaker(
bind=engine
)

event.listen(SessionFactory, 'deleted_to_detached', 
listener_bed_has_been_removed)

return SessionFactory


def listener_bed_has_been_removed(session, instance):
if type(instance) is not Bed:
return

bed_id = instance.id
house_id = instance.room.house.id  # this is NOT ALWAYS there. Depending on 
the order of the execution I guess

print("execute the service call to external service here bed_id {}, 
house_id {}".format(bed_id, house_id))


 

So my question(s):

   - Is there a clean way to always acces this parent's parent attribute?
  - If not, would be starting a new session and query it from the event 
  handler be an option? (is it not dangerous, because it seems to work)
 - Additional quirk, I am working within a transaction manager 
 (pyramid_tm) and ZopeTransactionExtension()
  
Thanks!


More information about the system:

SQLAlchemy 1.1.13

Python 3.5

Postgres 9.6

-- 
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 post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] Re: SQLAlchemy: backref and object loaded from DB not working?

2017-08-22 Thread cecemel
yep indeed, it works, I was doing something wrong. 

Thanks for all the help and sorry about late reply

On Thursday, August 17, 2017 at 9:57:39 PM UTC+2, cecemel wrote:
>
> Is the backref relationship supposed to work with objects loaded from the 
> DB?
>
>
> Here is the case: 
>
> Similar to the (doc/tutorial 
> <http://docs.sqlalchemy.org/en/rel_1_1/orm/backref.html?highlight=backref>) 
> I have the following classes:
>
> from sqlalchemy import Integer, ForeignKey, String, Columnfrom 
> sqlalchemy.ext.declarative import declarative_basefrom sqlalchemy.orm import 
> relationship
> Base = declarative_base()
> class User(Base):
> __tablename__ = 'user'
> id = Column(Integer, primary_key=True)
> name = Column(String)
>
> addresses = relationship("Address", back_populates="user")
> class Address(Base):
> __tablename__ = 'address'
> id = Column(Integer, primary_key=True)
> email = Column(String)
> user_id = Column(Integer, ForeignKey('user.id'))
>
> user = relationship("User", back_populates="addresses")
>
> When testing the snippet it works like expected:
>
> >>> u1 = User()>>> a1 = Address()>>> u1.addresses[]>>> print(a1.user)None
> >>> u1.addresses.append(a1)>>> u1.addresses[<__main__.Address object at 
> >>> 0x12a6ed0>]>>> a1.user<__main__.User object at 0x12a6590>
>
> But when doing the same with a user loaded to the db:
>
> >>> u1 = get_user_by_id_from_db(1)
> >>> a1 = Address()
> >>> u1.addresses # fair, the user didn't have any address.
> []
> >>> u1.addresses.append(a1)
> >>> u1.addresses # I didn't expect empty list!!
> []
>
> >>> a1.user
>
> None
>
> So my questions:
>
>- - is this supposed to work?
>- - if so, how can I make it work?
>- - if not, any workarounds proposed (and why not)?
>
> More information about the system:
>
> SQLAlchemy 1.1.13
>
> Python 3.5
>
> Postgres 9.6
>
>
>
> thanks again!
>
>
> Please let me know if any details miss.
>
>
>

-- 
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 post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.


[sqlalchemy] SQLAlchemy: backref and object loaded from DB not working?

2017-08-17 Thread cecemel


Is the backref relationship supposed to work with objects loaded from the 
DB?


Here is the case: 

Similar to the (doc/tutorial 
) 
I have the following classes:

from sqlalchemy import Integer, ForeignKey, String, Columnfrom 
sqlalchemy.ext.declarative import declarative_basefrom sqlalchemy.orm import 
relationship
Base = declarative_base()
class User(Base):
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
name = Column(String)

addresses = relationship("Address", back_populates="user")
class Address(Base):
__tablename__ = 'address'
id = Column(Integer, primary_key=True)
email = Column(String)
user_id = Column(Integer, ForeignKey('user.id'))

user = relationship("User", back_populates="addresses")

When testing the snippet it works like expected:

>>> u1 = User()>>> a1 = Address()>>> u1.addresses[]>>> print(a1.user)None
>>> u1.addresses.append(a1)>>> u1.addresses[<__main__.Address object at 
>>> 0x12a6ed0>]>>> a1.user<__main__.User object at 0x12a6590>

But when doing the same with a user loaded to the db:

>>> u1 = get_user_by_id_from_db(1)
>>> a1 = Address()
>>> u1.addresses # fair, the user didn't have any address.
[]
>>> u1.addresses.append(a1)
>>> u1.addresses # I didn't expect empty list!!
[]

>>> a1.user

None

So my questions:

   - - is this supposed to work?
   - - if so, how can I make it work?
   - - if not, any workarounds proposed (and why not)?

More information about the system:

SQLAlchemy 1.1.13

Python 3.5

Postgres 9.6



thanks again!


Please let me know if any details miss.


-- 
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 post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.