Hi MikeCo,
I tried your example, and got this error from SA 0.5.
ValueError: need more than 0 values to unpack
Darren
On Fri, 2009-01-09 at 20:33 -0800, MikeCo wrote:
> from sqlalchemy import *
> from sqlalchemy.orm import *
> from sqlalchemy.ext.declarative import declarative_base
>
> dbname = ''
> dburl = 'sqlite:///%s' % dbname
>
> # set up environment
> eng = create_engine(dburl, echo=False)
> meta = MetaData(bind=eng)
> Base = declarative_base(metadata=meta)
> Session = sessionmaker()
>
> # D self referential
> class D(Base):
> __tablename__ = 'D'
> id = Column(Integer, primary_key=True)
> id_d = Column(Integer, ForeignKey('D.id'))
> name = Column(Text)
> child = relation('D',
> backref=backref('parent', remote_side='D.id'))
> def __repr__(self):
> return '--<D> (id:%s->id_d:%s) %s --' % (self.id, self.id_d,
> self.name)
>
>
> def initdb():
> print '# create the database'
> meta.drop_all(checkfirst=True)
> meta.create_all()
> sess = Session()
>
> # insert some data for self-reference relationship
> for ddata in ('d-one', 'd-two'):
> dobj = D(name=ddata)
> sess.add(dobj)
> dobj2 = D(name=ddata+'-child1')
> dobj.child.append(dobj2)
> dobj2 = D(name=ddata+'-child2')
> dobj.child.append(dobj2)
> dobj3 = D(name=ddata+'-child2'+'-child3')
> dobj2.child.append(dobj3)
> sess.commit()
> sess.close()
>
> def printobj(obj, indent=''):
> # recursive list
> print '%s%s' % (indent, obj)
> if hasattr(obj, 'child'):
> for ch in obj.child:
> printobj(ch, indent=indent+' ')
>
> def listd():
> # retrieve and list
> sess = Session()
> print '# D->D'
> query = sess.query(D).filter(D.id_d==None).order_by(D.name)
> for dobj in query:
> printobj(dobj)
> sess.close()
>
> def tst3():
> # do stuff with self referential class D
> sess = Session()
> print "#switch parentage of name 'd-two-child1' to 'd-one'"
> print '# before'
> listd()
> child = sess.query(D).filter(D.name=='d-two-child1').one()
> parent = sess.query(D).filter(D.name=='d-one').one()
> eng.echo=True
> child.parent = parent
> child.name += ' MOVED'
> sess.commit()
> eng.echo=False
> print '# after'
> listd()
>
> initdb()
> tst3()
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---