Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

#36 Cannot use DEFAULT values defined in SQL.

closed-fixed
Oleg Broytman
General (126)
5
2008-03-07
2004-08-19
Andrew Bennetts
No

Given a table with a column like:

CREATE TABLE Foo (
datecreated timestamp NOT NULL DEFAULT
(CURRENT_TIMESTAMP AT TIME ZONE 'UTC'),
....
)

There is no way to utilise that default value when
constructing a Foo. e.g., if that is the only column
in Foo, it should be possible in Python to do:

f1 = Foo()

But even though the default value for the DateTimeCol's
default argument is NoDefault, SQLObject still insists
that an argument is passed to Foo's constructor.

For backwards compatibility, probably what should be
done is define a new magic value, e.g.
'NoDefaultRequired', so that the Foo class could be
defined as:

class Foo(SQLObject):
datecreated = DateTimeCol(notNull=True,
default=NoDefaultRequired)

Discussion

  • Logged In: YES
    user_id=142612

    Do you generate your table class from the SQL database?

    (See "Automatic Class Generation" in the SQLObject manual.)

    I don't know that such a complex default value could be imported by SQLObject from the SQL database schema, but it's technically possible.

    If you don't use automatic class generation, then try to define a default value in your python class, such as:

    from sqlobject import *

    class Foo(SQLObject):
    dt=DateTimeCol(default=func.NOW())

    foo=Foo()
    print foo.dt

    This does not work with PySQLite, but you can use datetime.datetime.today() instead.

     
  • Oleg Broytman
    Oleg Broytman
    2008-03-07

    • assigned_to: nobody --> phd
    • status: open --> closed-fixed
     
  • Oleg Broytman
    Oleg Broytman
    2008-03-07

    Logged In: YES
    user_id=4799
    Originator: NO

    Starting with SQLObject 0.9.4 one can define defaultSQL for a column and do not pass a value to the constructor.