admin管理员组

文章数量:1219981

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?

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?

Share Improve this question asked Oct 4, 2014 at 20:15 DeX3DeX3 5,5497 gold badges46 silver badges70 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 22

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
} );

example in typescript

import bookshelf from '../config/bookshelf';

class User extends bookshelf.Model<User> {
  get tableName(): string {
    return 'users';
  }
  get hasTimestamps(): boolean {
    return true;
  }
}

export default User;

本文标签: javascriptbookshelfjs timestamps not workingStack Overflow