nobrin / macaron

A simple O/R mapper for SQLite
http://nobrin.github.com/macaron/
MIT License
31 stars 8 forks source link

SQlite default CURRENT_TIMESTAMP won't work; is inserted literally #40

Open tripleee opened 8 years ago

tripleee commented 8 years ago

Macaron inserts the literal string CURRENT_TIMESTAMP if I declare a table with a field like this;

create table example(
    id          integer  primary key,
    timestamp   datetime default CURRENT_TIMESTAMP
);

and create a new Example() with no explicit timestamp value.

tripleee commented 8 years ago

I originally submitted a Bottle wishlist bug here; sorry for the mess.

tripleee commented 8 years ago

Repro steps.

bash$ sqlite3 temp.db
sqlite> create table example(
...> id integer primary key,
...> timestamp datetime default CURRENT_TIMESTAMP
...> );
sqlite> insert into example (id) values (1);
sqlite> select * from example;
1|2016-06-09 08:31:11

So far so good. CURRENT_TIMESTAMP gets substituted.

bash$ python
>>> import macaron
>>> class Example(macaron.Model): pass
...
>>> macaron.macaronage(dbfile='temp.db')
>>> flawed = Example.create()
>>> macaron.bake()
>>> [x.timestamp for x in Example.all()]
[u'2016-06-09 08:31:11', u'CURRENT_TIMESTAMP']
tripleee commented 8 years ago

A workaround is to define a before_create method in the Example class which populates self.timestamp with a suitable timestamp if it is None; but this obviously defeats the purpose of having SQlite supply a default.