views:

118

answers:

1
class MyObject(Entity):
    name = Field(Unicode(256), default=u'default name', nullable=False)

    using_options(shortnames=True)
    using_mapper_options(save_on_init=False)

    def __init__(self):
        self.name = None

I am using MySQL in this case, but have also checked against SQLite and I get the same result. It respects nullable, but ignores default entirely. I don't get any error messages, and it creates the tables just fine. I could go back through and add the defaults, but this is a serious pain that I would like to avoid if possible.

I've tried it with other Field types, but still nojoy.

+1  A: 

default keyword argument in SQLAlchemy is for Python runtime default value, it's used for INSERT statements. Use PassiveDefault() object as positional argument when you really need database level default.

Denis Otkidach
thank you sir. I was really confused by this.
Joel Hooks
I was confused too. But then I've found such behavior quite convenient: no need to update database schema when default is changed, unless it's used somewhere else without SQLAlchemy layer.
Denis Otkidach