Sorry, scrap the remark about primaryjoin ... inheritance. INheritance
wasn't the problem.

On Feb 5, 1:27 pm, lars van gemerden <l...@rational-it.com> wrote:
> OK, thank you,
>
> I went back to SQLA and came up with this for now (simplified):
> --------------------------------------------------------
> class Pairs(Base):
>     __tablename__ = 'Pairs'
>     name = Column(String(20), primary_key=True)
>     other_name = Column(String(20), ForeignKey('Pairs.name'), nullable
> = False)
>
>     other = relationship('Pairs',
>                           primaryjoin = 'Pairs.name ==
> Pairs.other_name',
>                           remote_side=[name])
>     def __init__(self, name):
>         self.name = name
>     def __repr__(self):
>         return "(%s, %s)" % (self.name, self.other.name)
>
> def pair(name1, name2):
>     p1, p2 = Pairs(name1), Pairs(name2)
>     p1.other_name = name2
>     p2.other_name = name1
>     return p1, p2
>
> if __name__ == '__main__':
>
>     p1, p2 = pair('apple', 'pear')
>     session.add_all([p1, p2])
>     session.commit()
>     for p in session.query(Pairs).all():
>         print p
>     assert p1.other.other is p1
> ------------------------------------------
> Note that there is no backref on "other" and that the primaryjoin is
> completely written out (otherwise a got a mysterious (to me) error,
> when using joined inheritance at the same time).
>
> This solution is key to my datamodel. Does anyone see any drawbacks?
>
> Cheers, Lars
>
> On Feb 5, 10:50 am, Gaëtan de Menten <gdemen...@gmail.com> wrote:
>
>
>
>
>
>
>
> > On 02/03/2012 12:08 PM, lars van gemerden wrote:
>
> > > I should probably make the pair method:
>
> > > def pair(name1, name2):
> > >      p1, p2 = Pairs(name1), Pairs(name2)
> > >      p1.other = p2
> > >      p2.other = p1
>
> > > On Feb 3, 11:57 am, lars van gemerden<l...@rational-it.com>  wrote:
> > >> Hi, I am trying to sote pairs in a table as follows:
>
> > >> #--------------------------------------------------------------------------
> > >>  ------------
> > >> from elixir import *
>
> > >> metadata.bind = "sqlite:///:memory:"
> > >> metadata.bind.echo = False
>
> > >> class Pairs(Entity):
> > >>      name = Field(String(50), primary_key = True)
> > >>      other = OneToOne('Pairs', inverse = 'other')
>
> > You can't have a OneToOne as inverse for a OneToOne, even less for
> > itself. Valid relationship pairs are:
>
> > ManyToOne - OneToOne
> > ManyToOne - OneToMany
> > ManyToMany - ManyToMany
>
> > In your case you want:
>
> > class Pairs(Entity):
> >      name = Field(String(50), primary_key = True)
> >      other1 = ManyToOne('Pairs', inverse = 'other2')
> >      other2 = OneToOne('Pairs', inverse = 'other1')
>
> > and if your database really only stores pairs, a property might make it
> > more elegant:
>
> >      @property
> >      def other(self):
> >          return self.other1 if self.other1 is not None else self.other2
>
> > As a side note, you probably do not want to use Elixir for a new
> > project, as Elixir is not maintained anymore.
>
> > -G.

-- 
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.

Reply via email to