views:

49

answers:

3

In sqlalchemy 0.5 i have a table defined like this one:

orders = Table('orders', metadata,
    Column('id', Integer, primary_key=True),
    Column('responsable', String(255)),
    Column('customer', String(255)),    
    Column('progressive', Integer),
    Column('date', Date),
    Column('exported', Boolean()),    
)

Is it possible to define only the customer and year of the date as unique ? The year isn't a column only a part of a date but it could be nice to have the year of the date and the customer to be a single key of the table. Is it possible in sqlalchemy 0.5 ?

Thanks

A: 

Quoth the documentation:

Multiple columns may be assigned the primary_key=True flag which denotes a multi-column primary key, known as a composite primary key.

msw
A: 

Without splitting the date field into a month field, a date field, and a year field, there really isn't a way to do what you're asking. It would (probably) be easier and simpler for you to include the whole date (month/day/year) in the composite primary; if (id, year) uniquely defines a record, then so will (id, date).

yarmiganosca
A: 

You can use a function in a constraint but it's not database independent. See here for details: http://stackoverflow.com/questions/1510018/compound-uniqueconstraint-with-a-function

However I would argue that the best thing to do would be to create a column for year. It will certainly be easier.

Singletoned