On Sep 18, 2007, at 9:58 AM, Rob Cowie wrote:

>
> Hi all,
>
> I am trying to model the following situation:
>
> I have an entity (a committee), which is stored in a single table.
> Mapping Committee object to this is trivial.
>
> Defined in this table is a foreign key (type) which references the pk
> of a table committee_type.
>
> I wish to map Committee obj to the join between tbl_committee and
> tbl_committee_type, something possible using join I think.
>
> However, I also want to enforce the constraint that any type added to
> a new committee object must be present in tbl_committee_type. I do not
> wish to be able to add a type and have that added to the table by
> sqlalchemy.
>
> For example, if type "audit" already exists as the pk in
> tbl_committee_type and I try to set type to "remun" on a new Committee
> object and flush, I want it to fail.
>
> I imagine this isn't too difficult, but at this stage, I cannot figure
> out how to do it. Any help would be gratefully appreciated.

So let me recap the parameters here:

1. Committee objects are stored in the "tbl_committee" table.
2. committee types are stored in the "tbl_committee_type"s table.
3. "tbl_committee_type" is pre-populated  with data;  the python  
program will *not* insert new rows in this table.

The quickest way I can think of for this looks like:

mapper(Committee, tbl_committee, properties={
        'type':relation(ComitteeType)
})

mapper(CommitteeType, tbl_committee_type)

class CommitteeType(object):
     def __init__(self):
         raise AssertionError("New CommitteeType objects cannot be  
instantiated")


__init__ is never called when CommitteeType objects are loaded from  
the DB (it uses __new__ instead).









--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to