这是我的 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 模型为“空”
我运行迁移,一切都很好,但是无法运行种子: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)
答案 0 :(得分:1)
编辑:
static get createdAtColumn () {
return null;
}
static get updatedAtColumn () {
return null;
}
将此2个函数添加到模型中,它应该可以工作:)
答案 1 :(得分:0)
要添加到先前的好答案中,当您随着应用程序的增长而拥有大量模型时,可以使上述解决方案自动化,而不必按照模型进行编写:
'use strict'
class NoTimestamp {
register (Model) {
Object.defineProperties(Model, {
createdAtColumn: {
get: () => null,
},
updatedAtColumn: {
get: () => null,
},
})
}
}
module.exports = NoTimestamp
感谢罗曼·兰兹(Romain Lanz),他向我展示了this解决方案。