Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bookshelf.js timestamps not working

I'm experimenting with bookshelf.js right now, and I created a sample table using the following knex migration:

exports.up = function(knex, Promise) {
  return knex.schema.createTable( "users", function( table ) {
        table.increments();
        table.timestamps();
        table.string( "email" );
    } );
};

I then defined a bookshelf.js model:

var User = bookshelf.Model.extend( {
    tableName: "users"
} );

and tried to save it:

var u = new User( { email: "[email protected]" } );
u.save();

Everything seems to work, and when I look at the database, the new user really was saved, however the timestamp-columns are NULL. Also calling u.timestamp() before calling u.save() doesn't seem to have any effect.

What am I doing wrong here?

like image 240
DeX3 Avatar asked Oct 04 '14 20:10

DeX3


1 Answers

Ha, I finally got it!

You have to tell the model to use timestamps like this:

var User = bookshelf.Model.extend( {
    tableName: "users",
    hasTimestamps: true
} );
like image 193
DeX3 Avatar answered Nov 02 '22 15:11

DeX3