【发布时间】:2018-01-31 09:49:36
【问题描述】:
我正在尝试设置一个具有 createdAt 和 updatedAt 列的模型,并希望sails 在条目更改时自动更新这些字段。目前我无法让它工作。试图查看文档,但他们没有提供有关如何设置的“指南”。
我的文件如下:
Users.js(模型)
module.exports = {
tableName: 'users',
primaryKey: 'id',
attributes: {
id: {
type:'number',
unique:true,
autoIncrement:true
},
name: {
type:'string'
},
email: {
type:'string'
},
password: {
type:'string'
},
profilePicture: {
type:'longtext'
},
location: {
type:'string'
},
createdAt: {
type:'number'
},
updatedAt:{
type: 'number'
}
}
};
config.model.js(将 migrate 设置为安全,这样每次启动时都不会清除表。)
migrate: 'safe',
attributes: {
createdAt: { type: 'number', autoCreatedAt: true, },
updatedAt: { type: 'number', autoUpdatedAt: true, },
id: { type: 'number', autoIncrement: true, },
},
在我的数据库 (MySQL) 中,我将 createdAt 和 updatedAt 列设置为 DATETIME。
但是,当我开始使用sails 时,表格已经存在,所以我不确定这些东西是否只有在您使用sails 创建表格时才有效。
【问题讨论】:
-
我有同样的问题,我没有找到任何解决方案。我认为这是一个已知的水线错误。我的解决方案是,当我使用 SQL 创建表时,这样做。