Hi Masetto,
On 26/03/2010 17:43, masetto wrote:
First of all, thanks for your answer :)
You are welcome.
# defInst.PlatformRel = [platf] # change this to
platf.definitions = defInst
I don't have any "definitions" attribute within the PlatformClass, i
suppose you mean the foreign key, isnt'it?
Yes you do:)
You have/had this in your model:
PlatformRel = relation(PlatformClass, backref="definitions")
"backref" creates a relation called "definitions" in the PlatformClass.
That is
platf.platformId_fk = defInst
However, this results in another error:
sqlalchemy.exc.InterfaceError: (InterfaceError) Error binding
parameter 1 - probably unsupported type. u'INSERT INTO platform
(platform, "definitionId_fk") VALUES (?, ?)' ['Microsoft Windows
2000', <ORM_Classes.DefinitionClass object at 0x8f5278c>]
I've played a little with it, then i've moved the relation() from
DefinitionClass to PlatformClass:
class PlatformClass(Base):
__tablename__ = 'platform'
platformId = Column(Integer, primary_key=True)
platform = Column(String)
platformId_fk = Column('definitionId_fk', Integer,
ForeignKey('definitions.defId'))
PlatformRel = relation(DefinitionClass, backref="platform")
and then:
platf.PlatformRel = defInst
Now i got the expected data! It WORKS :P Thanks Werner!
I think it should have worked the other way round too. But frankly I am
not an expert on SA, nor am I too comfortable just looking at code
fragments in an email.
But, i need to understand.. why now it's working?
From the doc:
"We are also free... to define the relationship()
<http://www.sqlalchemy.org/docs/reference/orm/mapping.html#sqlalchemy.orm.relationship>
only on one class and not the other. It is also possible to define two
separate relationship()
<http://www.sqlalchemy.org/docs/reference/orm/mapping.html#sqlalchemy.orm.relationship>
constructs for either direction, which is generally safe for
many-to-one and one-to-many relationships, but not for many-to-many
relationships."
Maybe i don't have well understood the role of the
relation()/relationship() function but, shouldn't be the same thing to
define the relation() within the DefinitionClass? I've only changed
the location of the relation() and now it works.
Can you kindly better explain me the role of the relationship() function?
Let me try and I hope that others will jump in if I say something
misleading.
relationship() (or its old but still valid equivalent relation()) or the
new name relationship() allow you to define relationships between two
tables.
You could do it in one of the two table like this (this is what I do
most of the time):
define it in PlatformClass:
PlatformRel = relation(DefinitionClass, backref="platform") - this sets
up both relationships from PlatformClass to DefinitionClass (a
one-to-many) and from DefinitionClass to PlatformClass (a many-to-one).
or you could "turn it around" and define it in DefinitionClass:
DefinitionRel = relation(PlatfromClass, backref="definitions")
or do it in both tables like this:
PlatformRel = relation(DefinitionClass) - sets up the one-to-many
DefinitionRel = relation(PlatformClass) - sets up the many-to-one
Mmm... please correct me if i'm wrong:
- "The relationship between the User and Address classes is defined
separately using the relationship()
<http://www.sqlalchemy.org/docs/reference/orm/mapping.html#sqlalchemy.orm.relationship>
function"
OK, and is the only way to define a relation between two tables.
- If i put relationship() in both classes i got a *bidirectional*
relationship
Or use "backref"
- "Because of the *placement* of the foreign key, from Address to User
it is *many to one*..." !!! Oh, is this the point, right? If, in the
same class, i define a foreign key AND a relationship() i create a
many to one relation with the linked table
- "..., and from User to Address it is *one to many*" - This is valid
only in the bidirectional case or it's "automatic" when i declare
somewhere foreign key + relationship() ?
Defining the foreign key does NOT setup/define a relation().
- Initially i've defined the foreign key in the PlatformClass and the
relation() in the DefinitionClass. Which type of relation i've created
in that way?
It does really not matter in which class you define the relation().
What type it will be I showed higher up, but you should also look at
"uselist" in relationship() doc.
You might also want to look at the doc of:
http://www.sqlalchemy.org/docs/reference/orm/mapping.html#sqlalchemy.orm.relationship
http://www.sqlalchemy.org/docs/reference/orm/mapping.html#sqlalchemy.orm.backref
I hope it helps and didn't cause more confusion.
Werner
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to sqlalch...@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.