[sqlalchemy] Re: Polymorphic union of two sibling classes (no real inheritance)

2010-07-30 Thread Kent
Hopefully you've got time to read a compliment: this polymorphism is
very cool (well, sqla in general).  Great work!

Kent

On Jul 29, 5:41 pm, Michael Bayer mike...@zzzcomputing.com wrote:
 On Jul 29, 2010, at 5:00 PM, Kent Bower wrote:

  Right.  I understand.  Thanks for pointing that out, you are correct.

  My bigger concern was getting the ArTranBase mapper correct.  Apparently 
  there is no need in this case to specify with_polymorphic= in the mapper. 
   Did I miss documentation on using 'polymorphic_union' without 
  with_polymorphic=?  That seems to be working, I was just looking for 
  confirmation that this is a supported use-case.

 that is probably correct.



  On 7/29/2010 4:51 PM, Michael Bayer wrote:
  What I meant was, if you want to say session.query(ArTranBase), which it 
  appears that you do, then you are querying against ArTranBase.

  Since it seems like you want the polymorphic_union here, when you query 
  ArTranBase and you want it to eagerly load trancode and paymenttype, 
  it would need to have a relation() on the ArTranBase mapper so that it 
  knows what to join.

  On Jul 29, 2010, at 4:46 PM, Kent wrote:

  This seems to work, but I didn't find examples of this.  Does this
  look correct (assuming there is no parent table in the database and
  all I really want is 2 'normal' mappers and a 3rd that performs a
  polymorphoric_union)?

  ==

  artran_union = polymorphic_union({
         'artran': artrans_table,
         'archive': artransarchive_table
     }, 'type', 'artran_union')

  artranbase_mapper = mapper(ArTranBase, artran_union,
     polymorphic_on=artran_union.c.type)

  #  ArTran
  --- #
  mapper(ArTran, artrans_table, inherits=artranbase_mapper,
     concrete=True, polymorphic_identity='artran',
     properties={'trancode': relation(TranCode,
                     cascade='refresh-expire,expunge', lazy=False),
                 'paymenttype': relation(PaymentType,
                     cascade='refresh-expire,expunge', lazy=False)}
     )

  #  ArTranArchive
  --- #
  mapper(ArTranArchive, artransarchive_table,
  inherits=artranbase_mapper,
     concrete=True, polymorphic_identity='archive',
     properties={'trancode': relation(TranCode,
                     cascade='refresh-expire,expunge', lazy=False),
                 'paymenttype': relation(PaymentType,
                     cascade='refresh-expire,expunge', lazy=False)}
     )

  On Jul 29, 4:20 pm, Kent Bowerk...@retailarchitects.com  wrote:

  No, in fact, there is no ArTranBase table at all.

  If I remove concrete inheritance, how do I issue a UNION of the two
  tables and have the objects polymorphically loaded?

  On 7/29/2010 4:18 PM, Michael Bayer wrote:

  On Jul 29, 2010, at 2:31 PM, Kent wrote:

  I'm getting a messy error that could be a bug, but is very likely
  related to my setup of a set of 2 polymorphic classes I am attempting
  to map.

  One entity is a transaction and the other is a transaction_archive
  record.  The table structure is therefore very similar for both tables
  and it seems to fit Concrete Table Inheritance, except there is no
  'parent' entity.  Rather, they are sister tables.

  What I have mostly works until I get into loading this union as a
  relation to another table... then I'm having problems.

  I couldn't clearly see the correct way to set up this when there is
  no real inheritance, but rather sister entities.

  Can you suggest how to correctly map these 2 tables?

  it looks fine to me except you're asking for eager loading, and if 
  you're querying from the ArTranBase you'd need to specify 
  relationship() at that level (as well as on each child).  Example 
  athttp://www.sqlalchemy.org/docs/mappers.html#using-relationships-with-

  OTOH if you are not querying from ArTranBase, remove the usage of 
  concrete inheritance altogether.

  
  artran_union = polymorphic_union({
          'artran': artrans_table,
          'archive': artransarchive_table
      }, 'type', 'artran_union')

  artranbase_mapper = mapper(ArTranBase, artran_union,
  with_polymorphic=('*', artran_union),
      polymorphic_on=artran_union.c.type,
  polymorphic_identity='ignored')

  #  ArTran
  --- #
  mapper(ArTran, artrans_table, inherits=artranbase_mapper,
      concrete=True, polymorphic_identity='artran',
      properties={'trancode': relation(TranCode,
                      cascade='refresh-expire,expunge', lazy=False),
                  'paymenttype': relation(PaymentType,
                      cascade='refresh-expire,expunge', lazy=False)}
      )

  #  ArTranArchive
  --- #
  mapper(ArTranArchive, artransarchive_table,
  

[sqlalchemy] Re: Polymorphic union of two sibling classes (no real inheritance)

2010-07-30 Thread Kent
I am having a problem when I'm specifying an order_by for a
relationship entity's column when the relationship is this
polymorphic_union.

orders = DBSession.query(Order)\
   .options(joinedload(Order.transactions))\
   .filter(Order.customerid==customerid)\
   .order_by(Order.orderdate, desc(ArTranBase.postdate)).all()

The SQL output is like this:

#
SELECT * ...
FROM orders,
 orderdetails orderdetails_1,
 ...
 ...,
/ polymorphic union (labeled 'anon_1') /
 (SELECT artrans.orderid AS orderid,
 artrans.status AS status,
 ...
 'artran' AS type
 FROM artrans
UNION ALL
 SELECT artransarchive.orderid AS orderid,
 artransarchive.status AS status,
 ...
 'archive' AS type
FROM artransarchive) anon_1,
 paymenttypes paymenttypes_1,
 ...
WHERE ... /* joins */
AND orders.orderid = anon_1.orderid(+)
... /* more joins */
ORDER BY orders.orderdate,
 artran_union.postdate DESC
#

*Notice the Problem:
 The ORDER BY is specifying the column 'artran_union.postdate' instead
of 'anon_1.postdate'. The joins are correctly using 'anon_1'.
 (or, perhaps the union itself should be labeled 'artran_union' or
'artran_union_1').




Mapping info:

#  Order
order_mapper = mapper(Order, orders_table,
properties={
   ...
   ...
  'transactions': relation(ArTranBase,
cascade='refresh-expire,expunge')
})

artran_union = polymorphic_union({
'artran': artrans_table,
'archive': artransarchive_table
}, 'type', 'artran_union')

#  ArTranBase
artranbase_mapper = mapper(ArTranBase, artran_union,
polymorphic_on=artran_union.c.type,
properties={'trancode': relation(TranCode,
cascade='refresh-expire,expunge', lazy=False),
'paymenttype': relation(PaymentType,
cascade='refresh-expire,expunge', lazy=False)}
)


#  ArTran
mapper(ArTran, artrans_table, inherits=artranbase_mapper,
concrete=True, polymorphic_identity='artran',
properties={'trancode': relation(TranCode,
cascade='refresh-expire,expunge', lazy=False),
'paymenttype': relation(PaymentType,
cascade='refresh-expire,expunge', lazy=False)}
)


#  ArTranArchive
mapper(ArTranArchive, artransarchive_table,
inherits=artranbase_mapper,
concrete=True, polymorphic_identity='archive',
properties={'trancode': relation(TranCode,
cascade='refresh-expire,expunge', lazy=False),
'paymenttype': relation(PaymentType,
cascade='refresh-expire,expunge', lazy=False)}
)

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



Re: [sqlalchemy] Re: Polymorphic union of two sibling classes (no real inheritance)

2010-07-30 Thread Michael Bayer

On Jul 30, 2010, at 9:51 AM, Kent wrote:

 I am having a problem when I'm specifying an order_by for a
 relationship entity's column when the relationship is this
 polymorphic_union.
 
 orders = DBSession.query(Order)\
   .options(joinedload(Order.transactions))\
   .filter(Order.customerid==customerid)\
   .order_by(Order.orderdate, desc(ArTranBase.postdate)).all()

you're making a common mistake here which is to ORDER BY an eagerly loaded 
relationship (probably our first FAQ entry).joinedload is strictly about 
loading collection contents and will alias itself so as not to interfere with 
the query's existing structure.   Here you'd want to 
query(Order).join(Order.transactions).options(contains_eager(Order.transactions)).order_by(...).
But the awkwardness here is that if Order.transactions is a collection, the 
order_by() you're doing doesn't make much sense - if Order 1 and Order 2 have 
the same date, but their collection of transactions are interleaved in terms of 
their dates, the ordering between them is somewhat meaningless.

If the order_by() is instead intended to determine the ordering just *within* 
the Order.transactions collections, you'd use the order_by option on the 
transactions relationship() itself.



 
 The SQL output is like this:
 
 #
 SELECT * ...
 FROM orders,
 orderdetails orderdetails_1,
 ...
 ...,
 / polymorphic union (labeled 'anon_1') /
 (SELECT artrans.orderid AS orderid,
 artrans.status AS status,
 ...
 'artran' AS type
 FROM artrans
 UNION ALL
 SELECT artransarchive.orderid AS orderid,
 artransarchive.status AS status,
 ...
 'archive' AS type
 FROM artransarchive) anon_1,
 paymenttypes paymenttypes_1,
 ...
 WHERE ... /* joins */
 AND orders.orderid = anon_1.orderid(+)
 ... /* more joins */
 ORDER BY orders.orderdate,
 artran_union.postdate DESC
 #
 
 *Notice the Problem:
 The ORDER BY is specifying the column 'artran_union.postdate' instead
 of 'anon_1.postdate'. The joins are correctly using 'anon_1'.
 (or, perhaps the union itself should be labeled 'artran_union' or
 'artran_union_1').
 
 
 
 
 Mapping info:
 
 #  Order
 order_mapper = mapper(Order, orders_table,
properties={
   ...
   ...
  'transactions': relation(ArTranBase,
cascade='refresh-expire,expunge')
})
 
 artran_union = polymorphic_union({
'artran': artrans_table,
'archive': artransarchive_table
}, 'type', 'artran_union')
 
 #  ArTranBase
 artranbase_mapper = mapper(ArTranBase, artran_union,
polymorphic_on=artran_union.c.type,
properties={'trancode': relation(TranCode,
cascade='refresh-expire,expunge', lazy=False),
'paymenttype': relation(PaymentType,
cascade='refresh-expire,expunge', lazy=False)}
)
 
 
 #  ArTran
 mapper(ArTran, artrans_table, inherits=artranbase_mapper,
concrete=True, polymorphic_identity='artran',
properties={'trancode': relation(TranCode,
cascade='refresh-expire,expunge', lazy=False),
'paymenttype': relation(PaymentType,
cascade='refresh-expire,expunge', lazy=False)}
)
 
 
 #  ArTranArchive
 mapper(ArTranArchive, artransarchive_table,
 inherits=artranbase_mapper,
concrete=True, polymorphic_identity='archive',
properties={'trancode': relation(TranCode,
cascade='refresh-expire,expunge', lazy=False),
'paymenttype': relation(PaymentType,
cascade='refresh-expire,expunge', lazy=False)}
)
 
 -- 
 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.
 

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



[sqlalchemy] Re: Polymorphic union of two sibling classes (no real inheritance)

2010-07-29 Thread Kent
This seems to work, but I didn't find examples of this.  Does this
look correct (assuming there is no parent table in the database and
all I really want is 2 'normal' mappers and a 3rd that performs a
polymorphoric_union)?

==

artran_union = polymorphic_union({
'artran': artrans_table,
'archive': artransarchive_table
}, 'type', 'artran_union')

artranbase_mapper = mapper(ArTranBase, artran_union,
polymorphic_on=artran_union.c.type)


#  ArTran
--- #
mapper(ArTran, artrans_table, inherits=artranbase_mapper,
concrete=True, polymorphic_identity='artran',
properties={'trancode': relation(TranCode,
cascade='refresh-expire,expunge', lazy=False),
'paymenttype': relation(PaymentType,
cascade='refresh-expire,expunge', lazy=False)}
)


#  ArTranArchive
--- #
mapper(ArTranArchive, artransarchive_table,
inherits=artranbase_mapper,
concrete=True, polymorphic_identity='archive',
properties={'trancode': relation(TranCode,
cascade='refresh-expire,expunge', lazy=False),
'paymenttype': relation(PaymentType,
cascade='refresh-expire,expunge', lazy=False)}
)



On Jul 29, 4:20 pm, Kent Bower k...@retailarchitects.com wrote:
 No, in fact, there is no ArTranBase table at all.

 If I remove concrete inheritance, how do I issue a UNION of the two
 tables and have the objects polymorphically loaded?

 On 7/29/2010 4:18 PM, Michael Bayer wrote:

  On Jul 29, 2010, at 2:31 PM, Kent wrote:

  I'm getting a messy error that could be a bug, but is very likely
  related to my setup of a set of 2 polymorphic classes I am attempting
  to map.

  One entity is a transaction and the other is a transaction_archive
  record.  The table structure is therefore very similar for both tables
  and it seems to fit Concrete Table Inheritance, except there is no
  'parent' entity.  Rather, they are sister tables.

  What I have mostly works until I get into loading this union as a
  relation to another table... then I'm having problems.

  I couldn't clearly see the correct way to set up this when there is
  no real inheritance, but rather sister entities.

  Can you suggest how to correctly map these 2 tables?

  it looks fine to me except you're asking for eager loading, and if you're 
  querying from the ArTranBase you'd need to specify relationship() at that 
  level (as well as on each child).  Example 
  athttp://www.sqlalchemy.org/docs/mappers.html#using-relationships-with-

  OTOH if you are not querying from ArTranBase, remove the usage of concrete 
  inheritance altogether.

  
  artran_union = polymorphic_union({
          'artran': artrans_table,
          'archive': artransarchive_table
      }, 'type', 'artran_union')

  artranbase_mapper = mapper(ArTranBase, artran_union,
  with_polymorphic=('*', artran_union),
      polymorphic_on=artran_union.c.type,
  polymorphic_identity='ignored')

  #  ArTran
  --- #
  mapper(ArTran, artrans_table, inherits=artranbase_mapper,
      concrete=True, polymorphic_identity='artran',
      properties={'trancode': relation(TranCode,
                      cascade='refresh-expire,expunge', lazy=False),
                  'paymenttype': relation(PaymentType,
                      cascade='refresh-expire,expunge', lazy=False)}
      )

  #  ArTranArchive
  --- #
  mapper(ArTranArchive, artransarchive_table,
  inherits=artranbase_mapper,
      concrete=True, polymorphic_identity='archive',
      properties={'trancode': relation(TranCode,
                      cascade='refresh-expire,expunge', lazy=False),
                  'paymenttype': relation(PaymentType,
                      cascade='refresh-expire,expunge', lazy=False)}
      )

  Thanks in advance.

  --
  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 
  athttp://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 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.



Re: [sqlalchemy] Re: Polymorphic union of two sibling classes (no real inheritance)

2010-07-29 Thread Michael Bayer
What I meant was, if you want to say session.query(ArTranBase), which it 
appears that you do, then you are querying against ArTranBase.  

Since it seems like you want the polymorphic_union here, when you query 
ArTranBase and you want it to eagerly load trancode and paymenttype, it 
would need to have a relation() on the ArTranBase mapper so that it knows what 
to join.




On Jul 29, 2010, at 4:46 PM, Kent wrote:

 This seems to work, but I didn't find examples of this.  Does this
 look correct (assuming there is no parent table in the database and
 all I really want is 2 'normal' mappers and a 3rd that performs a
 polymorphoric_union)?
 
 ==
 
 artran_union = polymorphic_union({
'artran': artrans_table,
'archive': artransarchive_table
}, 'type', 'artran_union')
 
 artranbase_mapper = mapper(ArTranBase, artran_union,
polymorphic_on=artran_union.c.type)
 
 
 #  ArTran
 --- #
 mapper(ArTran, artrans_table, inherits=artranbase_mapper,
concrete=True, polymorphic_identity='artran',
properties={'trancode': relation(TranCode,
cascade='refresh-expire,expunge', lazy=False),
'paymenttype': relation(PaymentType,
cascade='refresh-expire,expunge', lazy=False)}
)
 
 
 #  ArTranArchive
 --- #
 mapper(ArTranArchive, artransarchive_table,
 inherits=artranbase_mapper,
concrete=True, polymorphic_identity='archive',
properties={'trancode': relation(TranCode,
cascade='refresh-expire,expunge', lazy=False),
'paymenttype': relation(PaymentType,
cascade='refresh-expire,expunge', lazy=False)}
)
 
 
 
 On Jul 29, 4:20 pm, Kent Bower k...@retailarchitects.com wrote:
 No, in fact, there is no ArTranBase table at all.
 
 If I remove concrete inheritance, how do I issue a UNION of the two
 tables and have the objects polymorphically loaded?
 
 On 7/29/2010 4:18 PM, Michael Bayer wrote:
 
 On Jul 29, 2010, at 2:31 PM, Kent wrote:
 
 I'm getting a messy error that could be a bug, but is very likely
 related to my setup of a set of 2 polymorphic classes I am attempting
 to map.
 
 One entity is a transaction and the other is a transaction_archive
 record.  The table structure is therefore very similar for both tables
 and it seems to fit Concrete Table Inheritance, except there is no
 'parent' entity.  Rather, they are sister tables.
 
 What I have mostly works until I get into loading this union as a
 relation to another table... then I'm having problems.
 
 I couldn't clearly see the correct way to set up this when there is
 no real inheritance, but rather sister entities.
 
 Can you suggest how to correctly map these 2 tables?
 
 it looks fine to me except you're asking for eager loading, and if you're 
 querying from the ArTranBase you'd need to specify relationship() at that 
 level (as well as on each child).  Example 
 athttp://www.sqlalchemy.org/docs/mappers.html#using-relationships-with-
 
 OTOH if you are not querying from ArTranBase, remove the usage of concrete 
 inheritance altogether.
 
 
 artran_union = polymorphic_union({
 'artran': artrans_table,
 'archive': artransarchive_table
 }, 'type', 'artran_union')
 
 artranbase_mapper = mapper(ArTranBase, artran_union,
 with_polymorphic=('*', artran_union),
 polymorphic_on=artran_union.c.type,
 polymorphic_identity='ignored')
 
 #  ArTran
 --- #
 mapper(ArTran, artrans_table, inherits=artranbase_mapper,
 concrete=True, polymorphic_identity='artran',
 properties={'trancode': relation(TranCode,
 cascade='refresh-expire,expunge', lazy=False),
 'paymenttype': relation(PaymentType,
 cascade='refresh-expire,expunge', lazy=False)}
 )
 
 #  ArTranArchive
 --- #
 mapper(ArTranArchive, artransarchive_table,
 inherits=artranbase_mapper,
 concrete=True, polymorphic_identity='archive',
 properties={'trancode': relation(TranCode,
 cascade='refresh-expire,expunge', lazy=False),
 'paymenttype': relation(PaymentType,
 cascade='refresh-expire,expunge', lazy=False)}
 )
 
 Thanks in advance.
 
 --
 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 
 athttp://groups.google.com/group/sqlalchemy?hl=en.
 
 
 
 -- 
 You received this message because you are subscribed to the Google Groups 
 sqlalchemy group.
 To 

Re: [sqlalchemy] Re: Polymorphic union of two sibling classes (no real inheritance)

2010-07-29 Thread Kent Bower

Right.  I understand.  Thanks for pointing that out, you are correct.

My bigger concern was getting the ArTranBase mapper correct.  Apparently 
there is no need in this case to specify with_polymorphic= in the 
mapper.  Did I miss documentation on using 'polymorphic_union' without 
with_polymorphic=?  That seems to be working, I was just looking for 
confirmation that this is a supported use-case.




On 7/29/2010 4:51 PM, Michael Bayer wrote:

What I meant was, if you want to say session.query(ArTranBase), which it 
appears that you do, then you are querying against ArTranBase.

Since it seems like you want the polymorphic_union here, when you query ArTranBase and you want it 
to eagerly load trancode and paymenttype, it would need to have a 
relation() on the ArTranBase mapper so that it knows what to join.




On Jul 29, 2010, at 4:46 PM, Kent wrote:

   

This seems to work, but I didn't find examples of this.  Does this
look correct (assuming there is no parent table in the database and
all I really want is 2 'normal' mappers and a 3rd that performs a
polymorphoric_union)?

==

artran_union = polymorphic_union({
'artran': artrans_table,
'archive': artransarchive_table
}, 'type', 'artran_union')

artranbase_mapper = mapper(ArTranBase, artran_union,
polymorphic_on=artran_union.c.type)


#  ArTran
--- #
mapper(ArTran, artrans_table, inherits=artranbase_mapper,
concrete=True, polymorphic_identity='artran',
properties={'trancode': relation(TranCode,
cascade='refresh-expire,expunge', lazy=False),
'paymenttype': relation(PaymentType,
cascade='refresh-expire,expunge', lazy=False)}
)


#  ArTranArchive
--- #
mapper(ArTranArchive, artransarchive_table,
inherits=artranbase_mapper,
concrete=True, polymorphic_identity='archive',
properties={'trancode': relation(TranCode,
cascade='refresh-expire,expunge', lazy=False),
'paymenttype': relation(PaymentType,
cascade='refresh-expire,expunge', lazy=False)}
)



On Jul 29, 4:20 pm, Kent Bowerk...@retailarchitects.com  wrote:
 

No, in fact, there is no ArTranBase table at all.

If I remove concrete inheritance, how do I issue a UNION of the two
tables and have the objects polymorphically loaded?

On 7/29/2010 4:18 PM, Michael Bayer wrote:

   

On Jul 29, 2010, at 2:31 PM, Kent wrote:
 
   

I'm getting a messy error that could be a bug, but is very likely
related to my setup of a set of 2 polymorphic classes I am attempting
to map.
   
   

One entity is a transaction and the other is a transaction_archive
record.  The table structure is therefore very similar for both tables
and it seems to fit Concrete Table Inheritance, except there is no
'parent' entity.  Rather, they are sister tables.
   
   

What I have mostly works until I get into loading this union as a
relation to another table... then I'm having problems.
   
   

I couldn't clearly see the correct way to set up this when there is
no real inheritance, but rather sister entities.
   
   

Can you suggest how to correctly map these 2 tables?
   
   

it looks fine to me except you're asking for eager loading, and if you're 
querying from the ArTranBase you'd need to specify relationship() at that level 
(as well as on each child).  Example 
athttp://www.sqlalchemy.org/docs/mappers.html#using-relationships-with-
 
   

OTOH if you are not querying from ArTranBase, remove the usage of concrete 
inheritance altogether.
 
   


artran_union = polymorphic_union({
 'artran': artrans_table,
 'archive': artransarchive_table
 }, 'type', 'artran_union')
   
   

artranbase_mapper = mapper(ArTranBase, artran_union,
with_polymorphic=('*', artran_union),
 polymorphic_on=artran_union.c.type,
polymorphic_identity='ignored')
   
   

#  ArTran
--- #
mapper(ArTran, artrans_table, inherits=artranbase_mapper,
 concrete=True, polymorphic_identity='artran',
 properties={'trancode': relation(TranCode,
 cascade='refresh-expire,expunge', lazy=False),
 'paymenttype': relation(PaymentType,
 cascade='refresh-expire,expunge', lazy=False)}
 )
   
   

#  ArTranArchive
--- #
mapper(ArTranArchive, artransarchive_table,
inherits=artranbase_mapper,
 concrete=True, polymorphic_identity='archive',
 properties={'trancode': relation(TranCode,
 cascade='refresh-expire,expunge', lazy=False),
 

Re: [sqlalchemy] Re: Polymorphic union of two sibling classes (no real inheritance)

2010-07-29 Thread Michael Bayer

On Jul 29, 2010, at 5:00 PM, Kent Bower wrote:

 Right.  I understand.  Thanks for pointing that out, you are correct.
 
 My bigger concern was getting the ArTranBase mapper correct.  Apparently 
 there is no need in this case to specify with_polymorphic= in the mapper.  
 Did I miss documentation on using 'polymorphic_union' without 
 with_polymorphic=?  That seems to be working, I was just looking for 
 confirmation that this is a supported use-case.

that is probably correct.


 
 
 
 On 7/29/2010 4:51 PM, Michael Bayer wrote:
 What I meant was, if you want to say session.query(ArTranBase), which it 
 appears that you do, then you are querying against ArTranBase.
 
 Since it seems like you want the polymorphic_union here, when you query 
 ArTranBase and you want it to eagerly load trancode and paymenttype, it 
 would need to have a relation() on the ArTranBase mapper so that it knows 
 what to join.
 
 
 
 
 On Jul 29, 2010, at 4:46 PM, Kent wrote:
 
   
 This seems to work, but I didn't find examples of this.  Does this
 look correct (assuming there is no parent table in the database and
 all I really want is 2 'normal' mappers and a 3rd that performs a
 polymorphoric_union)?
 
 ==
 
 artran_union = polymorphic_union({
'artran': artrans_table,
'archive': artransarchive_table
}, 'type', 'artran_union')
 
 artranbase_mapper = mapper(ArTranBase, artran_union,
polymorphic_on=artran_union.c.type)
 
 
 #  ArTran
 --- #
 mapper(ArTran, artrans_table, inherits=artranbase_mapper,
concrete=True, polymorphic_identity='artran',
properties={'trancode': relation(TranCode,
cascade='refresh-expire,expunge', lazy=False),
'paymenttype': relation(PaymentType,
cascade='refresh-expire,expunge', lazy=False)}
)
 
 
 #  ArTranArchive
 --- #
 mapper(ArTranArchive, artransarchive_table,
 inherits=artranbase_mapper,
concrete=True, polymorphic_identity='archive',
properties={'trancode': relation(TranCode,
cascade='refresh-expire,expunge', lazy=False),
'paymenttype': relation(PaymentType,
cascade='refresh-expire,expunge', lazy=False)}
)
 
 
 
 On Jul 29, 4:20 pm, Kent Bowerk...@retailarchitects.com  wrote:
 
 No, in fact, there is no ArTranBase table at all.
 
 If I remove concrete inheritance, how do I issue a UNION of the two
 tables and have the objects polymorphically loaded?
 
 On 7/29/2010 4:18 PM, Michael Bayer wrote:
 
   
 On Jul 29, 2010, at 2:31 PM, Kent wrote:
 
   
 I'm getting a messy error that could be a bug, but is very likely
 related to my setup of a set of 2 polymorphic classes I am attempting
 to map.
   
   
 One entity is a transaction and the other is a transaction_archive
 record.  The table structure is therefore very similar for both tables
 and it seems to fit Concrete Table Inheritance, except there is no
 'parent' entity.  Rather, they are sister tables.
   
   
 What I have mostly works until I get into loading this union as a
 relation to another table... then I'm having problems.
   
   
 I couldn't clearly see the correct way to set up this when there is
 no real inheritance, but rather sister entities.
   
   
 Can you suggest how to correctly map these 2 tables?
   
   
 it looks fine to me except you're asking for eager loading, and if you're 
 querying from the ArTranBase you'd need to specify relationship() at that 
 level (as well as on each child).  Example 
 athttp://www.sqlalchemy.org/docs/mappers.html#using-relationships-with-
 
   
 OTOH if you are not querying from ArTranBase, remove the usage of 
 concrete inheritance altogether.
 
   
 
 artran_union = polymorphic_union({
 'artran': artrans_table,
 'archive': artransarchive_table
 }, 'type', 'artran_union')
   
   
 artranbase_mapper = mapper(ArTranBase, artran_union,
 with_polymorphic=('*', artran_union),
 polymorphic_on=artran_union.c.type,
 polymorphic_identity='ignored')
   
   
 #  ArTran
 --- #
 mapper(ArTran, artrans_table, inherits=artranbase_mapper,
 concrete=True, polymorphic_identity='artran',
 properties={'trancode': relation(TranCode,
 cascade='refresh-expire,expunge', lazy=False),
 'paymenttype': relation(PaymentType,
 cascade='refresh-expire,expunge', lazy=False)}
 )
   
   
 #  ArTranArchive
 --- #
 mapper(ArTranArchive, artransarchive_table,
 inherits=artranbase_mapper,
 concrete=True,