This is super! Thanks Michael

On Wednesday, November 27, 2013 7:49:23 AM UTC-8, Michael Bayer wrote:
>
>
> On Nov 27, 2013, at 1:23 AM, 史永宏 <shoot...@gmail.com <javascript:>> wrote:
>
> Hi,
> Is there a solution for this issue today? Now I have a single table 
> Inheritance in table A, however, the type information is stored in another 
> table B linked from A with foreign key...
> Now if I want to get polymorphic result when querying A, I got error ask 
> me to either use polymorphic or set enable_typechecks to False. 
> Any idea how to solve this? (After 3+ years)?
>
>
> this was solved two years ago, I failed to put a full migration doc for it 
> but here’s the changelog:
>
>
> http://docs.sqlalchemy.org/en/rel_0_9/changelog/changelog_07.html#change-dc3aefb201de941ad86706e78e661813
>
> *[orm] [feature] *polymorphic_on now accepts many new kinds of values:
>
>
>    - standalone expressions that aren’t otherwise mapped 
>    - column_property() objects 
>    - string names of any column_property() or attribute name of a mapped 
>    Column 
>
>
> I just wrote an example for someone but as usual I can’t find it, so here 
> it is again:
>
> from sqlalchemy import *
> from sqlalchemy.orm import *
> from sqlalchemy.ext.declarative import declarative_base
>
> Base = declarative_base()
>
> class AType(Base):
>     __tablename__ = 'atype'
>
>     id = Column(Integer, primary_key=True)
>     name = Column(String)
>
> class A(Base):
>     __tablename__ = 'a'
>
>     id = Column(Integer, primary_key=True)
>     data = Column(String)
>     type_id = Column(ForeignKey('atype.id'))
>     type_name = column_property(select([AType.name]).where(AType.id == id))
>     type = relationship(AType)
>
>     __mapper_args__ = {
>         "polymorphic_on": type_name,
>         "polymorphic_identity": "a"
>     }
>
> class ASub(A):
>     __tablename__ = 'asub'
>
>     id = Column(ForeignKey('a.id'), primary_key=True)
>     __mapper_args__ = {
>         "polymorphic_identity": "asub"
>     }
>
> e = create_engine("sqlite://", echo=True)
> Base.metadata.create_all(e)
>
> sess = Session(e)
>
> a_type, asub_type = AType(name="a"), AType(name="asub")
>
> sess.add_all([
>     A(data='a1', type=a_type),
>     ASub(data='asub1', type=asub_type),
>     ASub(data='asub2', type=asub_type),
>     A(data='a2', type=a_type),
> ])
> sess.commit()
>
> sess = Session(e)
> for a in sess.query(A):
>     print a.data, a.type
>
>
>
>
> On Thursday, May 20, 2010 12:22:34 PM UTC-7, Richard K wrote:
>>
>> Hi Timmy and Michael,
>>
>> I was wandering here about the same problem. I have a table that defines 
>> keys and value type (ie: key 'username', type 'unicode max 60' - that it is 
>> a constant).
>>
>> Then, I have the values table, which is polymorphic on value type and 
>> also has a fk to keys table. I think it is a bit lame to duplicate value 
>> type in those two tables, and also not that useful to use the polymorphic 
>> as a "static" value on each record of value table since I already have it 
>> on keys table. By having a fk to keys table, can't I get the polymorphic 
>> value from there?
>>
>> Thanks in advance,
>> Richard.
>>
>> On Wed, May 5, 2010 at 6:47 PM, Timmy Chan <timmy....@gmail.com> wrote:
>>
>>> thx, that is what i am wondering.  they are one-to-one, maybe thats good 
>>> enough for performance.  so there's no easy way to do this.
>>>
>>>
>>> On Wed, May 5, 2010 at 5:44 PM, Michael Bayer 
>>> <mik...@zzzcomputing.com>wrote:
>>>
>>>>
>>>> I meant SQL.    if you want to do polymorphic_on based on "a.type" it 
>>>> would at the very least need to issue:
>>>>
>>>> SELECT c.*, a.type JOIN b ON c.b_id = b.id JOIN a ON b.a_id=a
>>>>
>>>> which is an expensive way to get to where you're going.   It can be 
>>>> done of course but not through relation().
>>>>
>>>>
>>>> On May 5, 2010, at 5:36 PM, Timmy Chan wrote:
>>>>
>>>> "C"/"D" objects doesn't exist without "A" and "B"?  maybe walk up the 
>>>> graph:  C.b.a.type
>>>>
>>>> im not sure this schema is sensible, if it's really bad, please advise, 
>>>> im not attached to it!
>>>>
>>>> thax!
>>>>
>>>> On Wed, May 5, 2010 at 5:28 PM, Michael Bayer 
>>>> <mik...@zzzcomputing.com>wrote:
>>>>
>>>>> what query do you want to see when you select "C" and "D" objects ?
>>>>>
>>>>>
>>>>>
>>>>> On May 5, 2010, at 5:26 PM, Timmy Chan wrote:
>>>>>
>>>>> thx, is there a way to set that up, or something similar?
>>>>>
>>>>> after giving it thought, maybe polymorphic_on isn't what i need.  in 
>>>>> some sense, table "A" is similar to a generic container, and i want to 
>>>>> constrain the type of "C" depending on "A"'s type column.  is there a way 
>>>>> to do this?
>>>>>
>>>>> On Wed, May 5, 2010 at 5:15 PM, Michael Bayer <mik...@zzzcomputing.com
>>>>> > wrote:
>>>>>
>>>>>>
>>>>>> On May 5, 2010, at 5:01 PM, Timmy Chan wrote:
>>>>>>
>>>>>> i have 4 tables, a, b, c, d.
>>>>>>
>>>>>> a has one-to-many relation with b, b with one-to-one relationship 
>>>>>> with c, c is a polymorphic on a.type, with d being one of the 
>>>>>> polymorphic 
>>>>>> types.
>>>>>>
>>>>>> is there a way to implement this?
>>>>>>
>>>>>> details:
>>>>>>
>>>>>> this is what im trying to do in sqlalchemy:
>>>>>>
>>>>>>     a = Table('a', metadata,
>>>>>>               Column( 'id', Integer(), primary_key=True ),
>>>>>>               Column( 'type', UnicodeText() ) )
>>>>>>
>>>>>>     b = Table('b', metadata,
>>>>>>               Column( 'id', Integer(), primary_key=True ),
>>>>>>               Column( 'a_id', Integer(), ForeignKey('a.id') ) )
>>>>>>
>>>>>>     c = Table('c', metadata,
>>>>>>               Column( 'id', Integer(), primary_key=True ),
>>>>>>               Column( 'b_id', Integer(), ForeignKey('b.id') ),
>>>>>>               Column( 'class_id', Integer() ) )
>>>>>>       
>>>>>>     d = Table('d', metadata,
>>>>>>               Column( 'id', Integer(), primary_key=True ),
>>>>>>               Column( 'data', Integer() )
>>>>>>
>>>>>> mappers
>>>>>>
>>>>>>     mapper( A, a )
>>>>>>     mapper( B, b, properties={'a': relationship( A, 
>>>>>> uselist=False,backref='b',
>>>>>>            'c':relationship( C, uselist=False, backref='b') })
>>>>>>
>>>>>>     # Does a full join, does not work
>>>>>>     mapper( C, c, polymorphic_on = a.c.type )
>>>>>>     mapper( D, d, inherits=C, polymorphic_identity = "D" )
>>>>>>
>>>>>>
>>>>>> how can i change c to polymorphic on a, through the relationship?   
>>>>>> is there a way to sort by d.data?  (b-c/d is one-on-one).
>>>>>>
>>>>>>
>>>>>> this mapping is incorrect.     mapper(C) cannot be polymorphic on a 
>>>>>> table which is not part of its mapping, and mapper(C) does not contain 
>>>>>> an 
>>>>>> "inherits" keyword to that of A.   Usually the "polymorphic_on" setting 
>>>>>> is 
>>>>>> on the base-most mapper in the hierarchy and its not clear here which 
>>>>>> mapper you intend for that to be.
>>>>>>
>>>>>>
>>>>>>
>>>>>> -- 
>>>>>> You received this message because you are subscribed to the Google 
>>>>>> Groups "sqlalchemy" group.
>>>>>> To post to this group, send email to sqlal...@googlegroups.com.
>>>>>> To unsubscribe from this group, send email to 
>>>>>> sqlalchemy+...@googlegroups.com.
>>>>>> For more options, visit this group at 
>>>>>> http://groups.google.com/group/sqlalchemy?hl=en.
>>>>>>
>>>>>
>>>>>
>>>>> -- 
>>>>> You received this message because you are subscribed to the Google 
>>>>> Groups "sqlalchemy" group.
>>>>> To post to this group, send email to sqlal...@googlegroups.com.
>>>>> To unsubscribe from this group, send email to 
>>>>> sqlalchemy+...@googlegroups.com.
>>>>> For more options, visit this group at 
>>>>> http://groups.google.com/group/sqlalchemy?hl=en.
>>>>>
>>>>>
>>>>>
>>>>> -- 
>>>>> You received this message because you are subscribed to the Google 
>>>>> Groups "sqlalchemy" group.
>>>>> To post to this group, send email to sqlal...@googlegroups.com.
>>>>> To unsubscribe from this group, send email to 
>>>>> sqlalchemy+...@googlegroups.com.
>>>>> For more options, visit this group at 
>>>>> http://groups.google.com/group/sqlalchemy?hl=en.
>>>>>
>>>>
>>>>
>>>> -- 
>>>> You received this message because you are subscribed to the Google 
>>>> Groups "sqlalchemy" group.
>>>> To post to this group, send email to sqlal...@googlegroups.com.
>>>> To unsubscribe from this group, send email to 
>>>> sqlalchemy+...@googlegroups.com.
>>>> For more options, visit this group at 
>>>> http://groups.google.com/group/sqlalchemy?hl=en.
>>>>
>>>>
>>>>
>>>> -- 
>>>> You received this message because you are subscribed to the Google 
>>>> Groups "sqlalchemy" group.
>>>> To post to this group, send email to sqlal...@googlegroups.com.
>>>> To unsubscribe from this group, send email to 
>>>> sqlalchemy+...@googlegroups.com.
>>>> For more options, visit this group at 
>>>> http://groups.google.com/group/sqlalchemy?hl=en.
>>>>
>>>
>>>
>>> -- 
>>> You received this message because you are subscribed to the Google 
>>> Groups "sqlalchemy" group.
>>> To post to this group, send email to sqlal...@googlegroups.com.
>>> To unsubscribe from this group, send email to 
>>> sqlalchemy+...@googlegroups.com.
>>> For more options, visit this group at 
>>> http://groups.google.com/group/sqlalchemy?hl=en.
>>>
>>
>>
>> -- 
>> You received this message because you are subscribed to the Google Groups 
>> "sqlalchemy" group.
>> To post to this group, send email to sqlal...@googlegroups.com.
>> To unsubscribe from this group, send email to 
>> sqlalchemy+...@googlegroups.com.
>> For more options, visit this group at 
>> http://groups.google.com/group/sqlalchemy?hl=en.
>>
>
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to sqlalchemy+...@googlegroups.com <javascript:>.
> To post to this group, send email to sqlal...@googlegroups.com<javascript:>
> .
> Visit this group at http://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to