代码之家  ›  专栏  ›  技术社区  ›  Begueradj

字段列表中的“created\u at”列未知

  •  0
  • Begueradj  · 技术社区  · 7 年前

    这是我的 客户方案.js 文件:

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

    客户需求.js:

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

    这个 客户机 模型为“空”

    我运行迁移,一切正常,但无法运行种子: 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)
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   jsebastianms1    7 年前

    编辑:

      static get createdAtColumn () {
        return null;
      }
    
      static get updatedAtColumn () {
        return null;
      }
    

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

        2
  •  0
  •   Begueradj    7 年前

    要添加到前面的好答案中,当应用程序增长时有很多模型时,您可以自动化上面的解决方案,而不是按照模型编写:

    'use strict'
    
    class NoTimestamp {
      register (Model) {
        Object.defineProperties(Model, {
          createdAtColumn: {
            get: () => null,
          },
          updatedAtColumn: {
            get: () => null,
          },
        })
      }
    }
    
    module.exports = NoTimestamp
    

    感谢给我看的罗曼·兰兹 this 解决方案。

    推荐文章