admin 管理员组文章数量: 1184232
在安装sequelize-typescript过程中遇到的一些坑
注意安装的方法:
当你使用的是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
})
本文标签: Sequelize Typescript
版权声明:本文标题:sequelize-typescript使用的一些坑 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.roclinux.cn/b/1758609021a3088148.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论