Hi 

Thanks for your answer !

I understood that :
"The default behavior of relationship when an object is deleted that is 
referred to via another one with a foreign key is to set it to NULL" 

But if I use the DBMS to delete  the parent (*delete from node where id = 
1)* I have.

*ERROR: update or delete on table "node" violates foreign key constraint 
"node_parent_id_fkey" on table "node"*
*SQL state: 23503*
*Detail: Key (id)=(1) is still referenced from table "node"*

and I was wondering if there is a way to force  SA 1.0.4 ORM to react in 
the same way as the DBMS when I try to delete the parent using SA.

parent = session.query(Node).get(1)

session.delete(parent)
session.commit()


In my case should raise the same  IntegrityError.


Some hints ?


Regards 

G





On Tuesday, May 26, 2015 at 5:18:27 PM UTC+2, g wrote:
>
> Hi all
>
> With  SA 1.0.4 I can delete rows that i could not delete with SA 0.9.3 .
>
> MODEL.
>
> Base = declarative_base()
> class Node(Base):
>     __tablename__ = 'node'
>     id = Column(Integer, primary_key=True)
>     parent_id = Column(Integer, ForeignKey('node.id'))
>     data = Column(String(50))
>     children = relationship("Node",
>                 backref=backref('parent', remote_side=[id],
>                 ),lazy="dynamic"                        
>             )
>
> e = create_engine('postgresql+psycopg2://*****/test')
> Base.metadata.create_all(e)
> session = Session(e)
>
> QUERY AND DATA  ARE:
>
> query = session.query(
>                 Node.id,
>                 Node.parent_id,
>                 Node.data)
> results  = query.all()
> print results
>
> [(1, None, u'parent'), (2, 1, u'child')]
>
>
> NOW I TRY TO DELETE THE PARENT:
>
>
> parent = session.query(Node).get(1)
> print parent
> session.delete(parent)
> session.commit()
> results  = query.all()
> print results
>
>
> RESULT with SA 0.9.3
>
> sqlalchemy-0.9.3-py2.7.egg\sqlalchemy\engine\default.pyc in do_execute(self, 
> cursor, statement, parameters, context)    423     424     def 
> do_execute(self, cursor, statement, parameters, context=None):--> 425         
> cursor.execute(statement, parameters)    426     427     def 
> do_execute_no_params(self, cursor, statement, context=None):
> IntegrityError: (IntegrityError) update or delete on table "node" violates 
> foreign key constraint "node_parent_id_fkey" on table "node"
> DETAIL:  Key (id)=(1) is still referenced from table "node".
>  'DELETE FROM node WHERE node.id = %(id)s' {'id': 1}
>
>
>
> RESULT with SA 1.0.4: 
>
> ====================
>
> <__main__.Node object at 0x056BC5B0>
> [(2, None, u'child')]
>
>
> The parent is deleted and node.parent_id is set to null.
>
>
> What should I change in the relationship or model to have in SA 1.0.4 the 
> same behavior  as in SA 0.9.3 ?
>
>
> Regards 
>
> G
>
>
>
>
>
>
>
>

-- 
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 http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to