1

当设置sails模型以使用带有字符串主键(PK)的现有MySql数据库时,PK不再自动生成。我错过了什么还是我必须在 PK 生成中实现beforeCreate?如果我要实现 PK 生成,我不确定如何避免与遗留数据库中的现有密钥发生密钥冲突。

调用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 的遗留数据库?

4

1 回答 1

0

基于 Sails.js 文档 autoIncrement 必须始终为 type: 'integer'

自动递增

将属性设置为自动增量键。当向模型中添加新记录时,如果未指定此属性的值,则将通过将最近记录的值加一来生成它。注意:指定 autoIncrement 的属性应该始终是类型:'integer'。此外,请记住,支持级别因不同的数据存储而异。例如,MySQL 不允许每个表超过一个自增列。

于 2016-03-22T11:23:46.760 回答