【问题标题】:bookshelf.js timestamps not workingbookshelf.js 时间戳不起作用
【发布时间】:2014-10-04 20:15:24
【问题描述】:

我现在正在试验 bookshelf.js,并使用以下 knex 迁移创建了一个示例表:

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

然后我定义了一个 bookshelf.js 模型:

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

并试图保存它:

var u = new User( { email: "john.doe@example.com" } );
u.save();

似乎一切正常,当我查看数据库时,确实保存了新用户,但是时间戳列是NULL。在调用u.save() 之前也调用u.timestamp() 似乎没有任何效果。

我在这里做错了什么?

【问题讨论】:

    标签: javascript node.js bookshelf.js knex.js


    【解决方案1】:

    哈,我终于明白了!

    你必须告诉模型使用这样的时间戳:

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

    【讨论】:

    • 在添加 'hasTimestamps: true' 后,updated_at 工作正常,但它不响应 updatedData。它只在数据库中更新
    【解决方案2】:

    打字稿中的示例

    import bookshelf from '../config/bookshelf';
    
    class User extends bookshelf.Model<User> {
      get tableName(): string {
        return 'users';
      }
      get hasTimestamps(): boolean {
        return true;
      }
    }
    
    export default User;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多