its a bug, and theres a new ticket http://www.sqlalchemy.org/trac/ticket/1997 
with a small patch.

I would suggest maybe not using composites for this for now.   Most of what 
composite does you can accomplish using descriptors:

class Value(object):
    @property
    def custom_values(self):
        return CustomValue(self.v1, self.v2)

    @custom_values.setter
    def custom_values(self, value):
        self.v1 = value.v1
        self.v2 = value.v2

I considered replacing the mechanism of composite with the above type of thing 
in 0.7 but there were still some edge cases that composites seem to have.



On Dec 7, 2010, at 1:54 PM, Will wrote:

> from sqlalchemy import create_engine
> from sqlalchemy.orm import sessionmaker, mapper, relationship,
> composite
> from sqlalchemy.schema import Table, Column, MetaData, ForeignKey
> from sqlalchemy.sql import select
> from sqlalchemy.types import Integer, Text
> 
> engine = create_engine('sqlite:///')
> session = sessionmaker(bind=engine, autocommit=True)()
> metadata = MetaData(engine)
> 
> 
> # Tables
> descriptions_table = Table('descriptions', metadata,
>    Column('id', Integer, primary_key=True),
>    Column('d1', Text),
>    Column('d2', Text),
> )
> 
> values_table = Table('values', metadata,
>    Column('id', Integer, primary_key=True),
>    Column('description_id', Integer, ForeignKey('descriptions.id'),
>           nullable=False),
>    Column('v1', Text),
>    Column('v2', Text),
> )
> 
> desc_values = select(
>    [values_table, descriptions_table.c.d1, descriptions_table.c.d2],
>    descriptions_table.c.id == values_table.c.description_id
> ).alias('descriptions_values')
> 
> 
> # Classes
> class Descriptions(object):
>    pass
> 
> 
> class Values(object):
>    pass
> 
> 
> class CustomValues(list):
> 
>    def __init__(self, *args):
>        self.extend(args)
> 
>    def __composite_values__(self):
>        return self
> 
> 
> # Mappers
> mapper(Descriptions, descriptions_table, properties={
>    'values': relationship(Values, lazy='dynamic'),
>    'custom_descriptions': composite(CustomValues,
> descriptions_table.c.d1,
>                                     descriptions_table.c.d2),
> })
> 
> mapper(Values, desc_values, properties={
>    'custom_descriptions': composite(CustomValues, desc_values.c.v1,
>                                     desc_values.c.v2),
> })
> 
> 
> # Testing
> metadata.create_all()
> engine.echo = True
> 
> 
> descriptions = Descriptions()
> descriptions.custom_descriptions = CustomValues('Color', 'Number')
> 
> values1 = Values()
> values1.custom_values = CustomValues('Red', '5')
> 
> values2 = Values()
> values2.custom_values = CustomValues('Blue', '1')
> 
> descriptions.values.append(values1)
> descriptions.values.append(values2)
> 
> session.add(descriptions)
> session.flush()

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

Reply via email to