Hi,

Newbie question on relationships.
Consider the following code.

When deleting a child, I would like the parent *.children* attribute to be 
automatically updated by the orm.
It is not : am I doing something wrong or missed some configuration ?

Thanks a lot!

Pierre

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import sqlite3
from sqlalchemy import *
from sqlalchemy.types import *
from sqlalchemy.orm import *
from sqlalchemy.pool import NullPool
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()
METADATA = Base.metadata
DB_NAME = "example.db"

class Child(Base):

    __tablename__ = 'children'
    id = Column(Integer, primary_key=True)
    name = Column(String(20), nullable=False)

    # Many to One Child --- Parent
    timeslot_id = Column(Integer, ForeignKey('parent.id'))
    timeslot = relationship("Parent", backref="children")

    def __init__(self, name):
        self.name = name

    def __repr__(self):
        return self.name

class Parent(Base):

    __tablename__ = 'parent'
    id = Column(Integer, primary_key=True)
    name = Column(String(20), nullable=False)

    def __init__(self, name):
        self.name = name

    def __repr__(self):
        return self.name


if os.path.exists(DB_NAME):
    os.remove(DB_NAME)

engine = create_engine('sqlite:///%s' % DB_NAME,
                        connect_args={'detect_types': 
sqlite3.PARSE_DECLTYPES|
                                      sqlite3.PARSE_COLNAMES},
                        poolclass=NullPool,
                      )

def print_family(parent):

    print "%s's family:" % parent
    for child in parent.children:
        print "|____ %s" % child

session_maker = sessionmaker()
METADATA.create_all(engine)
session_maker.configure(bind=engine, expire_on_commit=False)

session = session_maker()

peter = Parent(name="Peter")
dan = Child(name="Dan")
jenny = Child(name="Jenny")

peter.children.append(dan)
peter.children.append(jenny)

session.add_all([dan, jenny, peter])
session.commit()

children = session.query(Child).all()
print children

parents = session.query(Parent).all()

for parent in parents:
    print_family(parent)

session.delete(dan)
session.commit()
print "Deleted Dan !"

for parent in parents:
    print_family(parent)
print "??? Dan is still in Peter's family..."

children = session.query(Child).all()
print children

print "... but not in the children list... :-("

-- 
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/groups/opt_out.

Reply via email to