在create子元素上递归递归关联错误

时间:2018-09-05 22:51:26

标签: node.js sequelize.js

嗨,我在Sequelize中有一个简单的架构,它给了我这个错误:

Executing (default): INSERT INTO "Nodes" ("id","name","createdAt","updatedAt") VALUES (4,'Node 1','2018-09-05 22:43:04.137 +00:00','2018-09-05 22:43:04.137 +00:00') RETURNING *;
Executing (default): INSERT INTO "Nodes" ("id","name","createdAt","updatedAt","fatherId") VALUES (DEFAULT,'Node 2','2018-09-05 22:43:04.142 +00:00','2018-09-05 22:43:04.142 +00:00',4) RETURNING *;

错误:

Unhandled rejection SequelizeUniqueConstraintError: Validation error
  

这是错误,而不是下一个ID,它的默认值为ID

模型如下:

'use strict';
module.exports = (sequelize, DataTypes) => {
  const Node = sequelize.define('Node', {
    name: DataTypes.STRING
  }, {});
  Node.associate = function(models) {
    Node.hasMany(models.Node, {as: 'Children', foreignKey: 'fatherId'});
    Node.hasOne(models.Node, {as: 'Father', foreignKey: 'id'});
  };
  return Node;
};

我要执行的代码是这样:

Node.create({
    name: "Node 1"
}).then(node => {
    node.createChild({
        name: "Node 2"
    }).then(subnode => {
        console.log(subnode);
    });
});

0 个答案:

没有答案