如何将Sequelize Auto中的引用转换为Sequelize中的关联?

时间:2017-06-16 19:00:51

标签: node.js sequelize.js

我使用Sequelize Auto生成我的数据库定义。在定义中,它使用"引用"定义哪些列是外键。属性。但是,

height:100vh;

如果我尝试在Sequelize查询中使用联接,我会收到:

$ cat models/account.js
/* jshint indent: 2 */

module.exports = function(sequelize, DataTypes) {
  return sequelize.define('account', {
    id: {
      type: DataTypes.BIGINT,
      allowNull: false,
      primaryKey: true,
      autoIncrement: true
    },
    parent_account_id: {
      type: DataTypes.BIGINT,
      allowNull: false,
      references: {
        model: 'account',
        key: 'id'
      }
    },
    master_account_id: {
      type: DataTypes.BIGINT,
      allowNull: false,
      references: {
        model: 'account',
        key: 'id'
      }
    },
    name: {
      type: DataTypes.STRING,
      allowNull: false
    },
    description: {
      type: DataTypes.STRING,
      allowNull: false,
      defaultValue: ''
    },
    enabled: {
      type: DataTypes.INTEGER(1),
      allowNull: false,
      defaultValue: '1'
    },
    account_type_id: {
      type: DataTypes.INTEGER(11),
      allowNull: false
    },
    billing_account_id: {
      type: DataTypes.BIGINT,
      allowNull: false,
      references: {
        model: 'account',
        key: 'id'
      }
    },
    contact_name: {
      type: DataTypes.STRING,
      allowNull: false,
      defaultValue: ''
    },
    contact_email: {
      type: DataTypes.STRING,
      allowNull: false,
      defaultValue: ''
    },
    data_feeds_path: {
      type: DataTypes.STRING,
      allowNull: false,
      defaultValue: ''
    },
    requests: {
      type: DataTypes.BIGINT,
      allowNull: false,
      defaultValue: '0'
    },
    true_impressions: {
      type: DataTypes.BIGINT,
      allowNull: false,
      defaultValue: '0'
    },
    clicks: {
      type: DataTypes.BIGINT,
      allowNull: false,
      defaultValue: '0'
    },
    actions: {
      type: DataTypes.BIGINT,
      allowNull: false,
      defaultValue: '0'
    },
    conversions: {
      type: DataTypes.BIGINT,
      allowNull: false,
      defaultValue: '0'
    },
    status_id: {
      type: DataTypes.BIGINT,
      allowNull: false,
      defaultValue: '1',
      references: {
        model: 'status',
        key: 'id'
      }
    },
    status_modified_date: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: '0001-01-01 00:00:00'
    },
    date_created: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: '0001-01-01 00:00:00'
    },
    date_created_user_id: {
      type: DataTypes.BIGINT,
      allowNull: false,
      defaultValue: '1',
      references: {
        model: 'user',
        key: 'id'
      }
    },
    date_modified: {
      type: DataTypes.DATE,
      allowNull: false,
      defaultValue: '0001-01-01 00:00:00'
    },
    date_modified_user_id: {
      type: DataTypes.BIGINT,
      allowNull: false,
      defaultValue: '1',
      references: {
        model: 'user',
        key: 'id'
      }
    },
    date_deleted: {
      type: DataTypes.DATE,
      allowNull: true
    },
    date_deleted_user_id: {
      type: DataTypes.BIGINT,
      allowNull: true,
      references: {
        model: 'user',
        key: 'id'
      }
    }
  }, {
    tableName: 'account'
  });
};

以下是我从Sequelize Auto文件导入模型定义后尝试用来添加关联的代码:

Unhandled rejection Error: X is not associated to Y!
    at Model.validateIncludedElement (/Library/WebServer/adstudio/node_modules/sequelize/lib/model.js:558:11)
    at /Library/WebServer/adstudio/node_modules/sequelize/lib/model.js:440:29
    at Array.map (native)
    at Model.validateIncludedElements (/Library/WebServer/adstudio/node_modules/sequelize/lib/model.js:436:37)
    at Model.aggregate (/Library/WebServer/adstudio/node_modules/sequelize/lib/model.js:1558:30)
    at Model.<anonymous> (/Library/WebServer/adstudio/node_modules/sequelize/lib/model.js:1624:17)
    at runCallback (timers.js:666:20)
    at tryOnImmediate (timers.js:639:5)

我已经尝试了几乎所有组合,并且关联没有正确定义,比如说account.id列是account_audit_log表的外键。我已经尝试了几乎所有的变化,并且无法看到它的可能性。

1 个答案:

答案 0 :(得分:-1)

不应将自我关联定义为

Person.hasOne(Person, {as: 'Father'}) 
// this will add the attribute FatherId to Person

虽然我没有使用sequelize auto