Re: [sqlalchemy] Selective relationship cascade

2010-12-02 Thread Michael Bayer

On Dec 2, 2010, at 6:48 AM, neurino wrote:

> Let's say I have acquisitions at regular intervals and each
> acquisition I get different measures.
> 
> Each meausre has a value and a numeric type id (1 = temperature, 2 =
> humidity ecc...)
> 
> I want user to insert text description for type id he knows, if he
> wants.
> 
> I end up having this 3 tables:
> 
> measures = Table('measure', metadata,
>Column('id', Integer, primary_key=True),
>Column('acq_id', Integer, ForeignKey('acquisitions.id'),
>nullable=False)
>Column('type_id', Integer, ForeignKey('types.id'),
>nullable=False),
>Column('value', Float, nullable=False),
>)
> 
> acquisitions = Table('acquisitions', metadata,
>Column('id', Integer, primary_key=True),
>Column('datetime', DateTime, nullable=False),
>)
> 
> types = Table('types', metadata,
>Column('id', Integer, primary_key=True, autoincrement=False),
>Column('desc', String, nullable=False),
>)
> 
> [class definitions...]
> 
> orm.mapper(Acquisition, acquisitions)
> orm.mapper(Type, types)
> orm.mapper(Measures, measure, properties={
>'type': orm.relationship(Type, backref='measures'),
>'acquisition': orm.relationship(Acquisition, backref='measures'),
>})
> 
> Now, while I want, deleting an acquisition, all child measures are
> deleted too,
> 
> I DON'T want any modification in data if the user edit types, for
> example:
> 
> - deletes a type (related measures are not deleted and their type_id
> is not changed in null value)
> 
> - edits the type.id (precedent associated measures are not updated to
> new type_id but mantain the old one).
> 
> How can I achieve this?
> I guess I should use cascade in relationship but can't figure how
> 
> Thanks for your support!

the cascade to delete child items when parent is deleted here would be:

'acquisition': orm.relationship(Acquisition, backref=backref('measures', 
cascade='all, delete-orphan'))




> 
> -- 
> 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] Selective relationship cascade

2010-12-02 Thread neurino
Let's say I have acquisitions at regular intervals and each
acquisition I get different measures.

Each meausre has a value and a numeric type id (1 = temperature, 2 =
humidity ecc...)

I want user to insert text description for type id he knows, if he
wants.

I end up having this 3 tables:

measures = Table('measure', metadata,
Column('id', Integer, primary_key=True),
Column('acq_id', Integer, ForeignKey('acquisitions.id'),
nullable=False)
Column('type_id', Integer, ForeignKey('types.id'),
nullable=False),
Column('value', Float, nullable=False),
)

acquisitions = Table('acquisitions', metadata,
Column('id', Integer, primary_key=True),
Column('datetime', DateTime, nullable=False),
)

types = Table('types', metadata,
Column('id', Integer, primary_key=True, autoincrement=False),
Column('desc', String, nullable=False),
)

[class definitions...]

orm.mapper(Acquisition, acquisitions)
orm.mapper(Type, types)
orm.mapper(Measures, measure, properties={
'type': orm.relationship(Type, backref='measures'),
'acquisition': orm.relationship(Acquisition, backref='measures'),
})

Now, while I want, deleting an acquisition, all child measures are
deleted too,

I DON'T want any modification in data if the user edit types, for
example:

 - deletes a type (related measures are not deleted and their type_id
is not changed in null value)

 - edits the type.id (precedent associated measures are not updated to
new type_id but mantain the old one).

How can I achieve this?
I guess I should use cascade in relationship but can't figure how

Thanks for your support!

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