注意安装的方法:
当你使用的是npm install sequelize --save
安装时默认安装的是sequelize最新版也就是V5以上的版本当你在安装npm install sequelize-typescript --save
时默认安装的是最新版,当你在将ts代码编译成js过程中会报一些关于sequelize-typescript包的一些错误,是由于当前sequelize的版本和sequelize-typescript的版本不兼容。解决方案如下:
1.卸载当前sequelize-typescript的版本(npm uninstall sequelize-typescript --save
)
2.执行npm install sequelize-typescript@next --save
命令
3.执行 npm install @types/validator @types/bluebird --save-dev
4.重新编译就不会报错
但是不要以为这样就解决问题了
当你在链接数据库的时候会发现莫名其妙的多了一些字段如creatAt
等等,还会发现自己的数据表莫名的多了个s解决办法
在@Table中加入timestamps: false,freezeTableName: true
@Table({
tableName: "project",
timestamps: false,
freezeTableName: true
})
因为当你在使用sequelize时会默认给你加一些字段,将你的表名变成第三人称所以需要手动的去修改
具体参数如下
var Bar = sequelize.define('bar', { /* bla */ }, {
// don't add the timestamp attributes (updatedAt, createdAt)
timestamps: false,
// don't delete database entries but set the newly added attribute deletedAt
// to the current date (when deletion was done). paranoid will only work if
// timestamps are enabled
paranoid: true,
// don't use camelcase for automatically added attributes but underscore style
// so updatedAt will be updated_at
underscored: true,
// disable the modification of table names; By default, sequelize will automatically
// transform all passed model names (first parameter of define) into plural.
// if you don't want that, set the following
freezeTableName: true,
// define the table's name
tableName: 'my_very_custom_table_name',
// Enable optimistic locking. When enabled, sequelize will add a version count attriubte
// to the model and throw an OptimisticLockingError error when stale instances are saved.
// Set to true or a string with the attribute name you want to use to enable.
version: true
})