OK, thanks, the order wasn't the problem, I am using this for the
initialization of my classes:
dicty= {'__tablename__': name,
'id': Column(Integer, ForeignKey(basename + '.id'),
primary_key = True),
'__mapper_args__': {'polymorphic_identity': name,
'inherit_condition': (id == classes[basename].id)}}
and as you said 'id' in the last line refers to the id() function. I
think I fixed it by changing the code to:
id_ = Column(Integer, ForeignKey(basename + '.id'), primary_key =
True)
out = {'__tablename__': name,
'id': id_,
'__mapper_args__': {'polymorphic_identity': name,
'inherit_condition': (id_ == classes[basename].id)}}
But I am getting a (maybe unrelated) error. Should this solution work?
Cheers, Lars
On Feb 23, 4:07 pm, Michael Bayer <[email protected]> wrote:
> On Feb 23, 2012, at 9:56 AM, lars van gemerden wrote:
>
>
>
>
>
>
>
>
>
> > Hi all,
>
> > I reproduced an error I was having with the code below (basically
> > pasted from the tutorial for joined inheritance) and added an
> > 'inherit_condition' to __mapper_args__ of the subclass. At that point
> > the code started throwing a sqlalchemy.exc.InterfaceError. The code
> > is:
>
> > class Person(Base):
> > __tablename__ = 'Person'
> > id = Column(Integer, primary_key=True)
> > discriminator = Column('type', String(50))
> > __mapper_args__ = {'polymorphic_on': discriminator}
>
> > class Engineer(Person):
> > __tablename__ = 'Engineer'
> > __mapper_args__ = {'polymorphic_identity': 'Engineer',
> > 'inherit_condition': (id == Person.id)}
> > id = Column(Integer, ForeignKey('Person.id'), primary_key=True)
>
> Well OK, right here, when you say "id", that's the Python built-in function
> "id()". This because the name "id" has not yet been assigned to point to
> your new Column object. The "class Engineer(Person):" is just another
> Python block, just like an "if <condition>:" statement. So yes, in that
> case you need to ensure "id" is pointing to that Column before you stick it
> into the "inherit_condition". This has nothing to do with the order of
> things being looked at in the actual "dict" of the class, just the order of
> how Python is assigning meaning to the name "id" within the "class:" block.
>
> The declarative tutorial doesn't have this bug, but yes I can see we might be
> better off illustrating __mapper_args__ at the bottom of the class
> declaration just to make this kind of error less likely.
>
>
>
> > Now we get to the problem: In my code I dynamically generate tables
> > with type(name, (base,), dicty). In dicty are the __mapper_args__ and
> > id Column. Because of the dictionary used I have no control over the
> > order of the arguments and that seems to cause my exception.
>
> I'm not getting how you'd have this issue there - you of course have total
> control over what goes into a dict() to use with type(), and in what order
> (it's only on the "taking things out" part of a dict that is not ordered).
> You should generate the Column object, then you put it in the dictionary.
> The "id" builtin function never gets involved:
>
> id = Column(...)
> dict = {
> 'id':id,
> '__mapper_args__':{'inherit_condition':id==Person.id}
>
>
>
>
>
>
>
> }
--
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.