hal_robertson wrote:
>
> Hmm... I must be missing something then in the mapping
>
> This is the error I'm getting:
>
> "AttributeError: 'Z' object has no attribute '__composite_values__'"
>
> How do I do the mapping so that the Z instances map to the respective
> composite tables correctly?
>
> I assume then I do not want to use "composite" in the mapping?
>
> #
> # MAPPING
> #
> mapper(A,composite_table_A,properties={
>     'a_z':composite(Z,composite_table_A.c.z1)})
> mapper(B,composite_table_B,properties={
>     'b_z':composite(Z,composite_table_B.c.z2)})
> mapper(C,composite_table_C,properties={
>     'c_z':composite
> (Z,composite_table_C.c.z1,composite_table_C.c.z2)})

it seems like you want a composite (though you can also do this using
custom accesor methods/descriptors too) - in the case of A and B your Z
instance maps one of its attributes to NULL:

class Z(object):
    def __init__(self,z1,z2):
        self.z1 = z1
        self.z2 = z2

    def __composite_values__(self):
        return self.z1, self.z2

    def __str__(self):
        return "Z(%r, %r)" % self.__composite_values__()


mapper(A,composite_table_A,properties={
    'a_z':composite(Z,composite_table_A.c.z1, null())})
mapper(B,composite_table_B,properties={
    'b_z':composite(Z, null(), composite_table_B.c.z2)})
mapper(C,composite_table_C,properties={
    'c_z':composite(Z,composite_table_C.c.z1,composite_table_C.c.z2)})


doing something like this:

    for a in session.query(A):
        print a.a1, a.a_z

    for b in session.query(B):
        print b.b1, b.b_z

    for c in session.query(C):
        print c.c1, c.c_z


gives you the appropriate query that maps Z.z1 or Z.z2 to NULL:

SELECT table_a.z1 AS table_a_z1, NULL AS anon_1, table_a.id AS table_a_id,
table_a.a1 AS table_a_a1
FROM table_a
aaa Z(u'a_z1', None)
SELECT NULL AS anon_1, table_b.z2 AS table_b_z2, table_b.id AS table_b_id,
table_b.b1 AS table_b_b1
FROM table_b
bbb Z(None, u'b_z2')
SELECT table_c.z1 AS table_c_z1, table_c.z2 AS table_c_z2, table_c.id AS
table_c_id, table_c.c1 AS table_c_c1
FROM table_c
ccc Z(u'c_z1', u'c_z2')



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

Reply via email to