Re: [sqlalchemy] Re: Elixir question

2012-02-05 Thread Gaëtan de Menten

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



[sqlalchemy] Re: Elixir question

2012-02-05 Thread lars van gemerden
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 gemerdenl...@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.



[sqlalchemy] Re: Elixir question

2012-02-05 Thread lars van gemerden
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 gemerdenl...@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.



[sqlalchemy] Re: Elixir question

2012-02-03 Thread lars van gemerden
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')

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

 def pair(name1, name2):
     p1, p2 = Pairs(name1), Pairs(name2)
     p1.other = p2

 if __name__ == '__main__':

     setup_all()
     create_all()

     pair('p1', 'p2')
 #-- 
 

 I am not very famiiar with SQL etc. but logically this seems possible
 (please orrect me if I am wrong). However I get the following error:
 
 File D:\Documents\Code\Eclipse\workspace\SQLAdata\src\tests.py, line
 22, in module
     setup_all()
   File build\bdist.win-amd64\egg\elixir\__init__.py, line 94, in
 setup_all

   File build\bdist.win-amd64\egg\elixir\entity.py, line 951, in
 setup_entities
   File build\bdist.win-amd64\egg\elixir\entity.py, line 198, in
 create_pk_cols
   File build\bdist.win-amd64\egg\elixir\entity.py, line 481, in
 call_builders
   File build\bdist.win-amd64\egg\elixir\relationships.py, line 448,
 in create_pk_cols
   File build\bdist.win-amd64\egg\elixir\relationships.py, line 791,
 in create_keys
   File build\bdist.win-amd64\egg\elixir\relationships.py, line 521,
 in inverse
 AssertionError: Relationships 'other' in entity 'Pair' and 'other' in
 entity 'Pair' cannot be inverse of each other because their types do
 not form a valid combination.
 

 Can anyone help me to understand the error and possibly fix it?

 Thanks in advance, Lars

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