javascript - Sequelize.JS many-to-many findAll not using join table -
i have following sequelize setup:
var sequelize = new sequelize('...', '...', '...', { host: '...', port: '3306', logging: true, define: { timestamps: true, paranoid: true } }); var period = sequelize.define('period', { name: { type: sequelize.string, allownull: false }, periodstart: { type: sequelize.integer }, periodend: { type: sequelize.integer } }); var material = sequelize.define('material', { name: { type: sequelize.string, allownull: false } }); var artefact = sequelize.define('artefact', { accession: { type: sequelize.string, allownull: false }, simplename: { type: sequelize.string }, dimensions: { type: sequelize.string }, thumbnail: { type: sequelize.text }, storagedescriptor: { type: sequelize.text }, description: { type: sequelize.text } }); artefact.hasmany(period); period.hasmany(artefact); artefact.hasmany(material); material.hasmany(artefact); period.hasmany(period, { as: 'children', foreignkey: 'parentid', usejunctiontable: false }); period.belongsto(period, { as: 'parent', foreignkey: 'parentid' }); material.hasmany(material, { as: 'children', foreignkey: 'parentid', usejunctiontable: false }); material.belongsto(material, { as: 'parent', foreignkey: 'parentid' }); sequelize.sync().success(function () { console.log('sync ok'); period.findall({ include: [ artefact ] }).success(function (periods) { console.log(json.stringify(periods, null, 4)); }); } ).error(function (err) { console.log('sync fail'); throw err; });
the sync()
call causes following create
statements:
create table if not exists `periods` (`name` varchar(255) not null, `periodstart` integer, `periodend` integer, `id` integer not null auto_increment , `createdat` datetime not null, `updatedat` datetime not null, `deletedat` datetime, `parentid` integer, primary key (`id`)) engine=innodb; create table if not exists `materials` (`name` varchar(255) not null, `id` integer not null auto_increment , `createdat` datetime not null, `updatedat` datetime not null, `deletedat` datetime, `parentid` integer, primary key (`id`)) engine=innodb; create table if not exists `artefacts` (`accession` varchar(255) not null, `simplename` varchar(255), `dimensions` varchar(255), `thumbnail` text, `storagedescriptor` text, `description` text, `id` integer not null auto_increment , `createdat` datetime not null, `updatedat` datetime not null, `deletedat` datetime, `categoryid` integer, `findlocationid` integer, `storelocationid` integer, primary key (`id`)) engine=innodb; create table if not exists `artefactsperiods` (`periodid` integer , `artefactid` integer , `createdat` datetime not null, `updatedat` datetime not null, `deletedat` datetime, primary key (`periodid`, `artefactid`)) engine=innodb; create table if not exists `artefactsmaterials` (`materialid` integer , `artefactid` integer , `createdat` datetime not null, `updatedat` datetime not null, `deletedat` datetime, primary key (`materialid`, `artefactid`)) engine=innodb;
which expect. however, call period.findall(...)
causes following sql , exception:
executing: select `periods`.*, `artefacts`.`accession` `artefacts.accession`, `artefacts`.`simplename` `artefacts.simplename`, `artefacts`.`dimensions` `artefacts.dimensions`, `artefacts`.`thumbnail` `artefacts.thumbnail`, `artefacts`.`storagedescriptor` `artefacts.storagedescriptor`, `artefacts`.`description` `artefacts.description`, `artefacts`.`id` `artefacts.id`, `artefacts`.`createdat` `artefacts.createdat`, `artefacts`.`updatedat` `artefacts.updatedat`, `artefacts`.`deletedat` `artefacts.deletedat`, `artefacts`.`categoryid` `artefacts.categoryid`, `artefacts`.`findlocationid` `artefacts.findlocationid`, `artefacts`.`storelocationid` `artefacts.storelocationid` `periods` left outer join `artefacts` `artefacts` on `periods`.`id` = `artefacts`.`periodid`; error: er_bad_field_error: unknown column 'artefacts.periodid' in 'on clause' @ query.sequence._packettoerror (/users/ndjm8/work/dev/sandbox/node_modules/mysql/lib/protocol/sequences/sequence.js:32:14) @ query.errorpacket (/users/ndjm8/work/dev/sandbox/node_modules/mysql/lib/protocol/sequences/query.js:82:18) @ protocol._parsepacket (/users/ndjm8/work/dev/sandbox/node_modules/mysql/lib/protocol/protocol.js:172:24) @ parser.write (/users/ndjm8/work/dev/sandbox/node_modules/mysql/lib/protocol/parser.js:62:12) @ protocol.write (/users/ndjm8/work/dev/sandbox/node_modules/mysql/lib/protocol/protocol.js:37:16) @ socket.ondata (stream.js:51:26) @ socket.eventemitter.emit (events.js:117:20) @ socket.<anonymous> (_stream_readable.js:736:14) @ socket.eventemitter.emit (events.js:92:17) @ emitreadable_ (_stream_readable.js:408:10) -------------------- @ query.sequence (/users/ndjm8/work/dev/sandbox/node_modules/mysql/lib/protocol/sequences/sequence.js:15:20) @ new query (/users/ndjm8/work/dev/sandbox/node_modules/mysql/lib/protocol/sequences/query.js:12:12) @ function.connection.createquery (/users/ndjm8/work/dev/sandbox/node_modules/mysql/lib/connection.js:48:10) @ connection.query (/users/ndjm8/work/dev/sandbox/node_modules/mysql/lib/connection.js:100:26) @ module.exports.query.run (/users/ndjm8/work/dev/sandbox/node_modules/sequelize/lib/dialects/mysql/query.js:26:17) @ module.exports.execqueueitem (/users/ndjm8/work/dev/sandbox/node_modules/sequelize/lib/dialects/mysql/connector-manager.js:313:21) @ /users/ndjm8/work/dev/sandbox/node_modules/sequelize/lib/dialects/mysql/connector-manager.js:269:25 @ adjustcallback (/users/ndjm8/work/dev/sandbox/node_modules/sequelize/node_modules/generic-pool/lib/generic-pool.js:187:7) @ dispense (/users/ndjm8/work/dev/sandbox/node_modules/sequelize/node_modules/generic-pool/lib/generic-pool.js:211:9) @ object.me.acquire (/users/ndjm8/work/dev/sandbox/node_modules/sequelize/node_modules/generic-pool/lib/generic-pool.js:260:5)
which appears isn't attempting use artefactsperiods
table created result of sync()
. i'm @ loss i'm doing wrong. self-referential relationships of period
, material
both function expected, many many don't work.
nevermind, after digging, known issue, discussed on:
Comments
Post a Comment