sequelizejs多对多关系

时间:2016-01-19 17:32:29

标签: mysql sequelize.js

我正在尝试在两个模型User和School之间建立双向,多对多的关系。有一个连接表, ' user_school'看起来像是:

+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| id         | mediumint(9) | NO   | PRI | NULL    | auto_increment |
| user_id    | mediumint(9) | NO   | MUL | NULL    |                |
| school_id  | mediumint(9) | NO   |     | NULL    |                |
| created_at | datetime     | YES  |     | NULL    |                |
| updated_at | datetime     | YES  |     | NULL    |                |
| updatedat  | datetime     | YES  |     | NULL    |                |
| createdat  | datetime     | YES  |     | NULL    |                |
+------------+--------------+------+-----+---------+----------------+

(我创建了各种createdAt和updatedAt字段,试图让模型正常工作。现在不是问题了。但我还是不确定正确的方法来定义这些字段,骆驼或蛇案。)

以下是我定义关系的方式:

School.belongsToMany(User, { through: 'user_school', foreignKey: 'school_id'});
User.belongsToMany(School, { through: 'user_school', foreignKey: 'user_id'});

第二种关系有效。我可以打电话给user.getSchools(),并获得与用户相关的学校。 但第一个关系并不起作用。当我打电话给school.getUsers()时,我收到这样的错误:

Executing (default): SELECT `id`, `email`, `name`, `description`, `address1`, `address2`, `city`, `state`, `zip`, `url`, `phone`, `deleted`, `approved`, `added_by` AS `addedBy`, `sub_count` AS `subCount`, `created_at` AS `createdAt`, `updated_at` AS `updatedAt` FROM `school` AS `school` WHERE `school`.`id` = '0';

Unhandled rejection TypeError: Cannot read property 'replace' of undefined
    at Object.module.exports.removeTicks (/Users/andrunix/code/learning/hapi-sequelize/node_modules/sequelize/lib/utils.js:345:13)
    at Object.module.exports.addTicks (/Users/andrunix/code/learning/hapi-sequelize/node_modules/sequelize/lib/utils.js:341:29)
    at Object.QueryGenerator.quoteIdentifier (/Users/andrunix/code/learning/hapi-sequelize/node_modules/sequelize/lib/dialects/mysql/query-generator.js:300:18)
    at Object.QueryGenerator.joinIncludeQuery (/Users/andrunix/code/learning/hapi-sequelize/node_modules/sequelize/lib/dialects/abstract/query-generator.js:1647:66)
    at generateJoinQueries (/Users/andrunix/code/learning/hapi-sequelize/node_modules/sequelize/lib/dialects/abstract/query-generator.js:1373:38)
    at Object.<anonymous> (/Users/andrunix/code/learning/hapi-sequelize/node_modules/sequelize/lib/dialects/abstract/query-generator.js:1411:27)
    at Array.forEach (native)
    at Object.QueryGenerator.selectQuery (/Users/andrunix/code/learning/hapi-sequelize/node_modules/sequelize/lib/dialects/abstract/query-generator.js:1410:10)
    at QueryInterface.select (/Users/andrunix/code/learning/hapi-sequelize/node_modules/sequelize/lib/query-interface.js:657:25)
    at null.<anonymous> (/Users/andrunix/code/learning/hapi-sequelize/node_modules/sequelize/lib/model.js:1377:32)
    at bound (domain.js:280:14)
    at runBound (domain.js:293:12)
    at tryCatcher (/Users/andrunix/code/learning/hapi-sequelize/node_modules/sequelize/node_modules/bluebird/js/release/util.js:11:23)
    at Promise._settlePromiseFromHandler (/Users/andrunix/code/learning/hapi-sequelize/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:489:31)
    at Promise._settlePromise (/Users/andrunix/code/learning/hapi-sequelize/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:546:18)
    at Promise._settlePromise0 (/Users/andrunix/code/learning/hapi-sequelize/node_modules/sequelize/node_modules/bluebird/js/release/promise.js:591:10)

如果我运行上面显示的SQL,查询可以正常工作。所以这部分逻辑似乎正在发挥作用。

如果我颠倒了顺序,则定义关系,如下所示:

User.belongsToMany(School, { through: 'user_school', foreignKey: 'user_id'});
School.belongsToMany(User, { through: 'user_school', foreignKey: 'school_id'});

这会导致&#34;破坏&#34;发生在另一个关系上。 school.getUsers()调用工作,user.getSchools()调用失败。

1 个答案:

答案 0 :(得分:3)

原来,无论出于什么原因,我需要在我的关系中添加'otherKey'。这是有效的:

User.belongsToMany(School, { through: 'user_school', foreignKey: 'user_id', otherKey: 'school_id'});
School.belongsToMany(User, { through: 'user_school', foreignKey: 'school_id', otherKey: 'user_id'});

感谢Mick Hansen在freenode上#sequelizejs的帮助。