On Nov 21, 2007, at 9:02 PM, Karlo Lozovina wrote:

>
> Hi guys, sorry for the vague subject, here is my problem. Let's say I
> have the following table:
>
> a = Table('aaa', meta,
>  Column('id', Integer, primary_key=True),
>  Column('id2', Integer))
>
> class A(object):
>  pass
>
> mapper(A, a)
>
> I want the default value of A.id2 to be that of a A.id. So, for
> example, unless I modify it, I would like the A.id2 to be sort of a
> duplicate primary key. I know I can find out what is A.id after
> committing my object to database and then setting A.id2 accordingly,
> but I was wondering can SA do this automatically for me? Maybe some
> kind of a special `default=` construct in the Column call?
>

you can write a default function that does this ( i need to add docs  
for column defaults that access the ExecutionContext):

def mydefault(ctx):
     return ctx.compiled_parameters['id']

a = Table('aaa', meta, Column('id', Integer, primary_key=True),  
Column('id2', Integer, default=mydefault))


now, the caveat:  the 'id' parameter must be available before the  
insert takes place.  that means if you are using mysql or sqlite, and  
relying upon auto-incrementing primary key columns, the 'id' column  
will not be present in the bind parameters before the insert.   if  
thats the case then youd have to write some kind of trigger or do a  
second UPDATE statement.

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