On May 22, 2008, at 12:23 PM, askel wrote:
>
> Hello everybody,
>
> I'm having hard time figuring out how or whether it is possible at all
> to use select statement that can access record to be inserted as
> column's default value.
>
> groups = Table('groups', meta,
> Column('id', Integer, primary_key=True),
> Column('prefix', String(32), nullable=False, unique=True)
> )
>
> accounts = Table('accounts', meta,
> Column('number', String(32), primary_key=True),
> Column('group_id', Integer, ForeignKey('groups.id'),
> nullable=False,
> default=select([groups.c.id], groups.c.prefix ==
> somefunc(current_accounts_record)))
> )
>
> Basically, what I need is to be able to access record/values to be
> inserted into accounts table to build correct select clause. I know
> how to do that on ORM level using MapperExtension but I want to
> enforce this on table level instead. And I realize that I can
> explicitly call my function to assign value to group_id at the time
> accounts.insert is executed but that doesn't smell good.
>
> Any help is greatly appreciated.
the function you pass to default can take an optional "context"
parameter which contains the current ExecutionContext. so write it
like this:
def my_default(ctx):
current_accounts_record = ctx.parameters['some_parameter']
return ctx.connection.scalar(select([groups.c.id],
groups.c.prefix ==somefunc(current_accounts_record)))
...
Column('mycolumn', Integer, default=my_default)
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---