【问题标题】:Sails.js store a primary keys in postresqlSails.js 在 postgresql 中存储主键
【发布时间】:2016-02-06 14:24:20
【问题描述】:

我正在尝试在使用sails.js 框架时添加设置postgresql 数据库。

但是,当我试图在我的数据库中保存一些模型值时,我得到了以下错误:error: null value in column "id" violates not-null constraint。有没有办法将水线自动创建的id存储在postgresql数据库中?

/models/user.js

module.exports = {
  connection: 'postgresqlServer',
  schema: true,

  attributes: {

    id: {
      type: 'integer',
      primaryKey: true
    },

    name: {
      type: 'string',
      required: true
    }
  }
};

/config/connection.js

module.exports.connections = {
  postgresqlServer: {
    adapter: 'sails-postgresql',
    host: 'localhost',
    user: 'root',
    password: '',
    database: 'myprojectdb',
    socketPath: '/Library/Application Support/Postgres/var-9.5',

    schema: true
  }
};

config/models.js

module.exports.models = {
  connection: 'sails-postgresql',
  migrate: 'alter'
};

【问题讨论】:

    标签: javascript postgresql sails.js waterline


    【解决方案1】:

    您应该为您的id 属性设置autoIncrement: true

    顺便说一句:Waterline 自动将id 添加到每个模型。如果需要,您可以使用 autoPK: false 禁用它。

    【讨论】:

    • 我已经完成并在 user.js 中添加了以下几行:id: { type: 'integer', autoIncrement: true, primaryKey: true, unique: true },现在有了这个结果: 错误:“id”列中的空值违反了非空约束
    • 如果您只是从列表中删除 id 字段会怎样?
    猜你喜欢
    • 1970-01-01
    • 2016-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 2014-08-19
    • 1970-01-01
    相关资源
    最近更新 更多