kvesteri / sqlalchemy-defaults

Smart SQLAlchemy defaults for lazy guys, like me.
Other
16 stars 7 forks source link

server_default is None for string types? #7

Open jacobsvante opened 9 years ago

jacobsvante commented 9 years ago

Taking the code from your example:

import sqlalchemy as sa
from sqlalchemy_defaults import Column
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class User(Base):
    __tablename__ = 'user'
    __lazy_options__ = {}
    id = Column(sa.Integer, primary_key=True)

    name = Column(
        sa.Unicode(255),
    )
    description = Column(
        sa.Unicode(255),
        default=u'',
    )

    is_admin = Column(
        sa.Boolean,
    )

    created_at = Column(
        sa.DateTime,
        auto_now=True
    )

    hobbies = Column(
        sa.Integer,
        min=1,
        max=4
    )

from sqlalchemy_defaults import make_lazy_configured

make_lazy_configured(sa.orm.mapper)

print('User.description.default:', User.description.default)
print('User.description.server_default:', User.description.server_default)

Running this code gives me:

User.description.default: ColumnDefault('')
User.description.server_default: None
jacobsvante commented 9 years ago

Ah, I looked through your tests and noticed that you run sa.orm.configure_mappers() after the models have been declared. I guess that's what made my example fail. Perhaps worth mentioning in the docs?

kvesteri commented 9 years ago

Yes definitely, I will update the docs.