default value from column

1,739 views
Skip to first unread message

ddarko

unread,
Jul 13, 2011, 7:37:14 AM7/13/11
to sqlalchemy
class AccountHistory(Base):
id = sa.Column(sa.types.Integer, primary_key=1)
date = sa.Column(sa.types.DateTime, nullable=0,
default=sa.func.current_timestamp())
type = sa.Column(sa.types.Integer, nullable=0, default=1)

def getdefault(self, name):
return self.....name....default ?

eg:
print(AccountHistory().getdefault('type'))
1


I would like to write a function that returns a default value defined
for that column in the table.
How does it make?

Will Weaver

unread,
Jul 13, 2011, 8:26:35 AM7/13/11
to sqlal...@googlegroups.com
I'm not positive about this but if you are talking about the integer column, all defaults have to be a string or some SQLAlchemy function so you'd want:

type = sa.Column(sa.types.Integer, nullable=0, default='1')
> --
> You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
> To post to this group, send email to sqlal...@googlegroups.com.
> To unsubscribe from this group, send email to sqlalchemy+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
>
>

ddarko

unread,
Jul 13, 2011, 8:40:19 AM7/13/11
to sqlalchemy
I do not want to define a column with a default value and use it. Just
I would like to write my own validator and I need to get the default
value of already-defined table.

SQLAlchemy 0.7.1

Mike Conley

unread,
Jul 13, 2011, 9:47:22 AM7/13/11
to sqlal...@googlegroups.com
given
Column('status', String(1), default='A')
Column('num', String(10), nullable=False, server_default='000')


try
        table.status.c.default.arg
for sqlalchemy managed arguments  
or
        table.num.c.server_default.arg
for database managed defaults    ""

Remember to test that the ".default" property actually exists; it won;t be there if the column has no default.


For the server_default to work you need to either specify it in your sqlalchemy table definition or reflect the table from the database so sqlalchemy picks it up.

You might also look at the inspector interface for schema in 
http://www.sqlalchemy.org/docs/core/schema.html


--
Mike Conley


ddarko

unread,
Jul 13, 2011, 10:19:55 AM7/13/11
to sqlalchemy
On Jul 13, 3:47 pm, Mike Conley <mconl...@gmail.com> wrote:
> given
> Column('status', String(1), default='A')
>
> try
>         table.status.c.default.arg

AttributeError: Neither 'InstrumentedAttribute' object nor
'Comparator' object has an attribute 'c'

I'm using the Declarative ORM Extension.
It could make a difference.

ddarko

unread,
Jul 13, 2011, 10:49:54 AM7/13/11
to sqlalchemy
I did it this way:

class AccountHistory(Base):
id = sa.Column(sa.types.Integer, primary_key=1)
date = sa.Column(sa.types.DateTime, nullable=0,
default=sa.func.current_timestamp())
type = sa.Column(sa.types.Integer, nullable=0, default=1)

def getdefault(self, name):
return
self._sa_class_manager.get(name).property.columns[0].default.arg

But perhaps is a better way ...

Mike Conley

unread,
Jul 13, 2011, 11:19:05 AM7/13/11
to sqlal...@googlegroups.com
Whatever gives a handle to the column should work. another example:

        def getdefault(self, name):
            default = self.__table__.columns[name].default
            if default:
                return default.arg
            else:
                return None


--
Mike Conley


Reply all
Reply to author
Forward
0 new messages