“字段列表”中的未知列 'created_at'

Unknown column 'created_at' in 'field list

这是我的 customer_schema.js 文件:

up () {                                                                                                                                        
    this.create('customers', (table) => {                                                                                                        
      table.increments()                                                                                                                         
      table.string('name', 30)                                                                                                                   
    })                                                                                                                                           
  }

CustomerSeed.js:

class CustomerSeeder {                                                                                                                           
  async run () {                                                                                                                                 
    const customer = await Factory                                                                                                               
      .model('App/Models/Customer')                                                                                                          
      .create()                                                                                                                              
    console.log('customer: ')                                                                                                                    
  }                                                                                                                                              
}

Customer.js模型是"empty "

我 运行 迁移,一切正常,但不能 运行 种子:adonis seed 抛出此错误消息:

code: 'ER_BAD_FIELD_ERROR',                                                                                                                    
  errno: 1054,                                                                                                                                   
  sqlMessage: "Unknown column 'created_at' in 'field list'",                                                                                     
  sqlState: '42S22',                                                                                                                             
  index: 0,                                                                                                                                      
  sql:                                                                                                                                           
   "insert into `customers` (`created_at`, `name`, `updated_at`) values ('2019-03-04 20:01:17', 'Peter Walsh', '2019-03-04 20:01:17')" }

为什么会这样?我什至没有在我的架构文件中声明 table.timestamps() 并且:


describe customers;                                                                                                                              
+-------+------------------+------+-----+---------+----------------+                                                                             
| Field | Type             | Null | Key | Default | Extra          |                                                                             
+-------+------------------+------+-----+---------+----------------+                                                                             
| id    | int(10) unsigned | NO   | PRI | NULL    | auto_increment |                                                                             
| name  | varchar(30)      | YES  |     | NULL    |                |                                                                             
+-------+------------------+------+-----+---------+----------------+                                                                             
2 rows in set (0.01 sec)

编辑:

  static get createdAtColumn () {
    return null;
  }

  static get updatedAtColumn () {
    return null;
  }

将这 2 个函数添加到您的模型中,它应该可以工作:)

为了补充之前的好答案,当应用程序增长时有很多模型时,您可以自动执行上述解决方案,而不是按照模型编写:

'use strict'

class NoTimestamp {
  register (Model) {
    Object.defineProperties(Model, {
      createdAtColumn: {
        get: () => null,
      },
      updatedAtColumn: {
        get: () => null,
      },
    })
  }
}

module.exports = NoTimestamp

感谢向我展示 this 解决方案的 Romain Lanz。