mysql - 将遗留的 mysql 数据库与新的 sails 应用程序一起使用

标签 mysql sails.js sails-mongo sails-postgresql

当设置 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/

相关文章:

mongodb - 获取 24 小时内在 sails js 中创建的记录

c# - 使用 Visual Studio 连接 MySQL 数据库

mysql - 如何添加多列并显示最大值?

python - 索引错误 : tuple index out of range -- String formatting

sails.js - sails 10.x 水线 : What attribute type to use for Mongo objectID

javascript - Sails 1 - 违反一致性 : Attempting to tear down a datastore (`default` ) which is not currently registered with this adapter

php - 如何在 Azure 上运行的服务器上安装 MySQL PHP 驱动程序?

javascript - 如何上传文件 - sails.js

image - 我看不到刚刚上传的图片 [sails.js]

javascript - 如果收件人电子邮件错误,Nodemailer 中会出现错误