2009/4/9 mhearne808 <[email protected]>:

> Say I want to have a one-to-many auto-referential relationship.  I
> thought from reading a previous post on a one-to-one auto-referential
> relationship, that I could do something like this:
>
> class Person(Entity):
>    name = Field(Text)
>    children = OneToMany('Person')
>    mother = OneToOne('Person')
>
>    def __repr__(self):
>        return '<Person %s>' % (self.name)
>
> ...
> and then, ...
>
> mom = Person(name='Mom')
> baby1 = Person(name='Baby1')
> baby2 = Person(name='Baby2')
> baby1.mother = mom
> baby2.mother = mom
> print baby1.mother
> print mom.children
>
> which gives me:
> <Person Mom>
> Traceback (most recent call last):
>  File "./testmodel.py", line 22, in <module>
>    print mom.children
> AttributeError: 'Person' object has no attribute 'children'
>
> Is there a way to set this up so that a Person can have many children,
> and a child only one mother?

You have two errors in there. First, you don't call setup_all() after
all your entities have been defined.

But even if you did, you would receive an exception like:

Exception: Couldn't find any relationship in '<class
'__main__.Person'>' which match as inverse of the 'children'
relationship defined in the '<class '__main__.Person'>' entity. If you
are using inheritance you might need to specify inverse relationships
manually by using the 'inverse' argument.

In other words, a OneToMany relationship always needs a ManyToOne
relationship (as its inverse) and can't use a OneToOne relationship.


-- 
Gaëtan de Menten
http://openhex.org

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"SQLElixir" 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/sqlelixir?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to