当设置 sails 模型以使用带有字符串主键 (PK) 的现有 MySql 数据库时,不再自动生成 PK。
我是否遗漏了什么,或者我是否必须在 beforeCreate
中实现 PK 生成?如果我要实现 PK 生成,我不确定如何避免与遗留数据库中的现有 key 发生 key 冲突。
调用 Cat.create().exec(console.log)
会抛出以下错误:
Invalid attributes sent to User:
• id
• `undefined` should be a string (instead of "null", which is a object)
• "required" validation rule failed for input: null
模型有以下代码:
module.exports = {
connection: 'laraschMysql',
schema: true,
identity: 'Contest',
tableName: 'Contest',
migrate: 'safe',
autoPK: false,
autoCreatedAt: false,
autoUpdatedAt: false,
autoUpdatedAt: false,
attributes: {
id: {
type: 'string', //varchar(255) in the db
autoIncrement:true, // not working?
primaryKey: true,
required: true
}
}
更一般地说:在新的 sails 应用程序中使用遗留数据库时,常见的问题/绊脚石/问题是什么?
是否有任何好的指南(sails 文档似乎没有太彻底地涵盖该主题)来设置带有 sails 的遗留数据库?
最佳答案
基于 Sails.js 文档 autoIncrement 必须始终是类型:'integer'
autoIncrement
Sets up the attribute as an auto-increment key. When a new record is added to the model, if a value for this attribute is not specified, it will be generated by incrementing the most recent record's value by one. Note: Attributes which specify autoIncrement should always be of type: 'integer'. Also, bear in mind that the level of support varies across different datastores. For instance, MySQL will not allow more than one auto-incrementing column per table.
关于mysql - 将遗留的 mysql 数据库与新的 sails 应用程序一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35834273/