tags:

views:

307

answers:

2

Hi All,

I've reflecting a load of tables in an exiting mysql db. I want to express that any columns of a certain name in any table default to datetime.now(). However naively looping through the tables and columns and just setting default on those I find that have a certain name doesn't work, When doing session.add(); session.flush() I get the following error:

AttributeError: 'builtin_function_or_method' object has no attribute '__visit_name__'

This would seem to be tied up in the call to _set_parent (and the this self._init_items(*toinit) at line 721 in sqlalchemy.schema.

Does anyone know if there's a way to do this without either going through all of my reflected tables and adding Column(..) lines every where all doing the same thing or resorting to really ugly hacks?

+3  A: 

OK, I've fixed this and it's a moderately horrid hack, in that I've using an internal method. You need to do:

from sqlalchemy.schema import ColumnDefault
#....
ColumnDefault(callable)._set_parent(column)

This produces the required behaviour. Note that you can also pass ColumnDefault the kwarg for_update=True and you'll this will change the behaviour to change on UPDATE rather than insert.

I feel dirty :-(

Ben Ford
+2  A: 

Another option is to override the columns that need defaults while reflecting:

Table('some_table', metadata,
    Column('create_time', DateTime, default=datetime.now),
    autoload=True)

Unfortunately you currently can't reflect the datatype and set SQLAlchemy side default at the same time. One could argue though that the datatype is part of the interface definition and so the redundancy doesn't create a maintenance issue - if the columns datatype changes you most likely need to change the default value function too.

Ants Aasma
Thanks Ants, this would definitely be my prefered approach on a per table bases, however I'm reflecting a number of tables at once and I want a rule to apply to all table with this specific column name, so I'm marking my answer below as the prefered (if icky) answer.
Ben Ford