Re: [sqlalchemy] Cascade delete-orphan: reattach child to another parent

2018-03-31 Thread Serhii Mozghovyi
Many thanks!

On Wednesday, March 28, 2018 at 6:42:16 PM UTC+3, Mike Bayer wrote:
>
> the backrefs intentionally don't keep fanning deep into object graph 
> for this kind of thing, so if you want it to go one hop further you 
> can add an event to do that directly: 
>
> from sqlalchemy import event 
> from sqlalchemy.orm import attributes 
>
>
> @event.listens_for(Address.user, "set") 
> def _intercept_set(target, value, oldvalue, initiator): 
>
> if isinstance(oldvalue, User) and "address" in oldvalue.__dict__: 
> attributes.set_committed_value(oldvalue, "address", None) 
>
>
> the "set_committed_value" is to avoid triggering any new events which 
> will cause a recursion overflow. 
>
>
>
> On Wed, Mar 28, 2018 at 10:49 AM, Serhii Mozghovyi  > wrote: 
> > Is it possible to make child objects (many-to-one side) delete itself 
> from 
> > the old parent's collection when it is added to a different parent? 
> > See the file attached. The old parent remains unaware that he doesn't 
> have 
> > this child anymore. 
> > 
> > P.S. session.expire() is an obvious solution but too heavy. I expect 
> some 
> > event-based collection synchronization, much like backref 
> (back_populates) 
> > connected collections. 
> > 
> > -- 
> > 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.


Re: [sqlalchemy] Cascade delete-orphan: reattach child to another parent

2018-03-28 Thread Mike Bayer
the backrefs intentionally don't keep fanning deep into object graph
for this kind of thing, so if you want it to go one hop further you
can add an event to do that directly:

from sqlalchemy import event
from sqlalchemy.orm import attributes


@event.listens_for(Address.user, "set")
def _intercept_set(target, value, oldvalue, initiator):

if isinstance(oldvalue, User) and "address" in oldvalue.__dict__:
attributes.set_committed_value(oldvalue, "address", None)


the "set_committed_value" is to avoid triggering any new events which
will cause a recursion overflow.



On Wed, Mar 28, 2018 at 10:49 AM, Serhii Mozghovyi  wrote:
> Is it possible to make child objects (many-to-one side) delete itself from
> the old parent's collection when it is added to a different parent?
> See the file attached. The old parent remains unaware that he doesn't have
> this child anymore.
>
> P.S. session.expire() is an obvious solution but too heavy. I expect some
> event-based collection synchronization, much like backref (back_populates)
> connected collections.
>
> --
> 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 - 
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] Cascade delete-orphan: reattach child to another parent

2018-03-28 Thread Serhii Mozghovyi
Is it possible to make child objects (many-to-one side) delete itself from 
the old parent's collection when it is added to a different parent? 
See the file attached. The old parent remains unaware that he doesn't have 
this child anymore.

P.S. session.expire() is an obvious solution but too heavy. I expect some 
event-based collection synchronization, much like backref (back_populates) 
connected collections.

-- 
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.
from sqlalchemy import create_engine, MetaData, Column, String, ForeignKey
from sqlalchemy.orm import sessionmaker, relationship
from sqlalchemy.ext.declarative import declarative_base


engine = create_engine('mysql://user@localhost/probe')

Base = declarative_base(bind=engine)

Session = sessionmaker(bind=engine)
session = Session()


class User(Base):
__tablename__ = 'user'
name = Column(String(10), primary_key=True)
address = relationship('Address', uselist=False, cascade='all,delete-orphan', back_populates='user')


class Address(Base):
__tablename__ = 'address'
addr = Column(String(20))
username = Column(ForeignKey(User.name), primary_key=True)
user = relationship('User', back_populates='address')


if __name__ == '__main__':
joe, jef = session.query(User).all()
if joe.name != 'joe':
joe, jef = jef, joe

assert joe.address is not None
assert jef.address is None

jef.address = joe.address

# HERE IS THE PROBLEM: it's expected to become None
assert joe.address is not None
session.flush()
assert joe.address is not None  # not even flush() helps

# Here's how sample objects were created:
# Base.metadata.create_all()
# joe = User(name='joe')
# jef = User(name='jef')
# session.add_all((joe, jef))
# joe.address = Address(addr='joe str')
# session.commit()


[sqlalchemy] cascade delete-orphan

2009-01-15 Thread GustaV

Hi all,
I try to set up a many-to-many relation with an association object.

But I want something not usual: I want the child object deleted when
not owned by any parent anymore.
This is for a messages/recipients relation: the message is useless
when everybody removed it from its mailbox!

I tried that, but it doesn't work:

class Parent(meta.DeclarativeBase):
id = Column(types.Integer, primary_key=True)

class Child(meta.DeclarativeBase):
id = Column(types.Integer, primary_key=True)

class Assoc(meta.DeclarativeBase):
p_id = Column(types.Integer,
  ForeignKey(Parent.id))
c_id = Column(types.Integer,
  ForeignKey(Parent.id))

parent = relation(Parent,
  backref=backref('children',
cascade='all, delete-orphan'))
child = relation(Child,
 backref='parents',
 cascade='delete-orphan')

I expect child = relation(Child, backref='parents', cascade='delete-
orphan') to forward deletes to child when it is an orphan. But it
looks like it forward the delete even if it is not an orphan yet...

It that configuration:

   p1 = Parent()
   p2 = Parent()
   c = Child()
   assoc1 = Assoc(parent=p1, child=c)
   assoc2 = Assoc(parent=p2, child=c)

p1.children = [ ] will lead to:
- delete assoc1 (ok)
- delete c (not ok)
- update assoc2.c_id = null (not ok)

So why is it not really a delete-orphan? :)

Thanks

GustaV
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---



[sqlalchemy] Cascade delete-orphan: child deleted althought still referenced ?

2006-12-19 Thread Nebur

I create a child table and a parent table. The latter holds a relation
to the child with delete-orphan cascade.
When I delete _the first_ of my 2 parents, the child is immediately
deleted, too.
***
This is my first attempt to use delete-orphan. I don't dare to report
it as a bug,
for the case I misunderstand the SA API.

The complete code:


from sqlalchemy import *

class Child(object):
def __init__(self, name):
self.childname = name
class Parent(object):
def __init__(self, name):
self.parentname = name

db = create_engine(mysql://[EMAIL PROTECTED]/test_cascade)
db.echo = True
session = create_session()

metadata = BoundMetaData(db)
t_parent = Table(parent,metadata,
Column(id,Integer,primary_key=True),
Column(parentname,String()),
Column(child_id,Integer,ForeignKey(child.id)),
mysql_engine=InnoDB,
)
t_child = Table(child,metadata,
Column(id,Integer,primary_key=True),
Column(childname,String()),
mysql_engine=InnoDB,
)
metadata.create_all()
mapper(Child, t_child)
mapper(Parent, t_parent, properties={
mychild:relation(Child,cascade=delete-orphan),
})

# create a child + 2 parents:
aChild = Child(aChild);session.save(aChild)
aParent1 = Parent(aParent1); aParent1.mychild=aChild;
session.save(aParent1)
aParent2 = Parent(aParent2); aParent2.mychild=aChild;
session.save(aParent2)
session.flush()

# it doesn't matter whether I create a new session here, or continue
with the old one

# delete first parent:
session.delete(aParent1)

# With InnoDB, the next flush raises Foreign Key constraint failure
# because aChild is deleted - while aParent2 ist still referencing it !
session.flush() #fails

# with MyISAM, I can still go ahead ...
print session.query(Child).get_by(childname=aChild) # the child is
deleted
# ... and try to delete second parent:
session.delete(aParent2) #
# next flush raises an error because aChild is not attached to session
anymore
session.flush()

It this a Bug, or did I misuse the ORM... ?
Regards
 Ruben

-
Versions: sqlalchemy 0.3.3 and 0.3.1 and MySQL5.


--~--~-~--~~~---~--~~
 You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To post to this group, send email to sqlalchemy@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~--~~~~--~~--~--~---