Sequelize Association在Create

时间:2018-12-03 16:19:24

标签: javascript node.js sequelize.js

我正在尝试在NodeJS API中与sequ​​elize进行简单的关联,想法很简单,我想在同一时刻创建一个人及其类型。我尝试遵循这些文档,但是当create函数被称为Sequelize时会引发错误

“ TypeError:无法读取未定义的属性'getTableName'”

下面是使用的代码:

模型

Person.js

import Sequelize from 'sequelize';

export default (sequelize) => {
  const Person = sequelize.define('person', {
    email: { type: Sequelize.STRING },
    nickname: { type: Sequelize.STRING(60) },
    fullname: { type: Sequelize.STRING(60) },
    observation: { type: Sequelize.TEXT },
  }, { underscored: true, freezeTableName: true });

  Person.associate = (models) => {
    Person.hasOne(models.PersonType, {
      foreignKey: 'person_id',
    });
  };

  return Person;
};

PersonType.js

import Sequelize from 'sequelize';

export default (sequelize, DataTypes) => {
  const PersonType = sequelize.define('person_type', {
    type: { type: Sequelize.STRING(14), unique: true },
  }, { underscored: true, freezeTableName: true });

  PersonType.associate = (models) => {
    PersonType.belongsTo(models.Person, {
      foreignKey: 'person_id',
    });
  };

  return PersonType;
};

index.js

import Sequelize from 'sequelize';
import console from 'console';

const database = process.env.DB_URL;

const sequelize = new Sequelize(database, {
  dialect: 'postgres',
  underscored: true,
});

const models = {
   Person: sequelize.import('./person'),
   PersonType: sequelize.import('./personType'),
};

Object.keys(models).forEach((modelName) => {
  if ('associate' in models[modelName]) {
    models[modelName].associate(models);
  }
});

models.sequelize = sequelize;
models.Sequelize = Sequelize;

export default models;

我尝试执行该操作的文件:

import models from '../models';

export default class PersonOperations {
  constructor(db) {
    this.db = db; // db here is the sequelize model of Person
  }

  create(person) {
    return this.db.create({
      email: person.email,
      nickname: person.nickname,
      observation: person.observation,
      personType: {
        type: person.personType.type,
      },
    }, {
      include: [{
        model: models.PersonType,
        include: [person.personType],
      }],
    });
  }

错误:

TypeError: Cannot read property 'getTableName' of undefined
    at Function._validateIncludedElement (/home/~/node_modules/sequelize/lib/model.js:465:30)
    at options.include.options.include.map.include (/home/~/node_modules/sequelize/lib/model.js:395:37)
    at Array.map (<anonymous>)
    at Function._validateIncludedElements (/home/~/node_modules/sequelize/lib/model.js:390:39)

我已经尝试了很多发现的东西,但是没有一个能帮助我。

  • 我正在使用freezeTableName,我已经尝试删除它,删除数据库并再次创建,但是问题仍然存在。

1 个答案:

答案 0 :(得分:0)

只需注释掉Person.js中的以下代码块并运行代码

Person.associate = (models) => {
    Person.hasOne(models.PersonType, {
      foreignKey: 'person_id',
    });
};
  

错误原因:

     

在这里,您正在使用创建循环依赖关系   models.PersonType内的models.Person,然后使用后   models.Person内的models.PersonType

hasOnebelongsTo都是相同的,只是diff是( READ ):

Player.belongsTo(Team)  // `teamId` will be added on Player / Source model
Coach.hasOne(Team)  // `coachId` will be added on Team / Target model