Is there a way to access current record from DefaultGenerator? I have
balances table that collect information on daily basis. Whenever a new
record inserted in that table it has to "inherit" some information
from previous day record. Of course, I can implement that using SQL-
server capabilities but I'd rather not go that way.
So basically, I need an access to a record to be inserted when
generator is asked for a value.
Thanks
http://www.sqlalchemy.org/docs/metadata.myt#metadata_defaults_oninsert
Thanks for reply but I know about that. The question is how do I
access a record that requires default value from withing that
function. Something like the following:
def prev_balance():
new_bal = get_record_that_is_being_inserted() # <=== that is what
I'm looking for
return
build_subquery_to_retrieve_previous_balance_value(new_bal.account,
new_bal.date) # <=== I know how to do that part
balances = Table('balances', meta,
Column('account', Integer, ForeignKey('accounts.id'),
nullable=False, primary_key=True),
Column('date', Date, nullable=False, primary_key=True),
Column('value', Numeric(12,2), nullable=False,default=prev_balance),
)
On Mar 1, 8:04 pm, "Michael Bayer" <zzz...@gmail.com> wrote:
> people ask for this function occasionally, what i dont like about it
> is that it would greatly complicate things just to produce
> functionality that isnt really needed.
It wasn't my intention to ask anybody to implement such a
functionality. I thought that I simply failed to find it.
> for example, if you want one
> of the columns in your INSERT to be driven off the values in the other
> columns, why not just prepare that data *outside* of the call to
> sqlalchemy's insert() ?
In my case data cannot be prepared outside, well at least not unless I
begin transaction, fetch data I need then insert new record and commit
transaction. But that doesn't look quite right for me since in plain
SQL it would be just one INSERT statement with SELECT. I guess it's
just a price I have to pay for using ORM and not dealing with SQL
directly.
Anyway, thanks for answering.
im looking at the default code and it wouldnt be too hard to give it
access to the "execution context" and make the parameters available
and all that, it probably should be that way anyway since there is an
"ExecutionContext" object. id want to refactor some things.
http://www.sqlalchemy.org/docs/metadata.myt#metadata_defaults_oninsert