balderdashy / balderdashy/sails

Many-to-Many association, auto generated model throws an error on sails.hooks.orm.reload.

Open
#4,618 2 comments 0 reactions 0 assignees View on GitHub
does this answer your question? orm
Dominant language
JavaScript
Stars
22.8k
Forks
1.9k
PR merge metrics
No merged PRs in 30d

Description

**Waterline version**: 0.13.x
**Sails version**: 1.0.2
**Node version**: 8.12.x
**NPM version**: 6.4.1
**Operating system**: MAC



I faced an issue while integration testing (using mocha) of my controllers. While using sails.hooks.orm.reload() in my `after` block, I was getting the following error:

Failed to reload ORM hook. Details: Error: Failed to reinitialize ORM because the `initialize()` method of the ORM hook returned an error.
Details:
userError: The attribute `morty_ricks` on the `morty_ricks__rick_mortys` model is an association but also specifies a type property. This is not needed and willbe automatically determined.

My models:
Rick.js:
module.exports = {

attributes: {
name: {
type: 'string'
},
mortys: {
collection: 'morty',
via: 'ricks'
}
},

};

Morty.js:
module.exports = {
attributes: {
name: {
type: 'string'
},
ricks: {
collection: 'rick',
via: 'mortys'
}
},
};

A simple many-to-many association. A workaround for this problem was by defining 'through' property in my associations and creating a custom through table `Citadel.js` as mentioned in through assoication [docs](https://sailsjs.com/documentation/concepts/models-and-orm/associations/through-associations)

To reproduce the issue:
Make a new sails project with the above models.
npm install sails-mysql --save
in config.models.js keep `migrate: alter`.
Go into console mode and try sails.hooks.orm.reload();
npx sails c
sails.hooks.orm.reload();

Contributor guide

Open the contributing guide

Research direction

Reproduce the problem with Rick.js and Morty.js, sails-mysql, and config.models.js set to migrate: alter. Start by running npx sails c and sails.hooks.orm.reload(), then inspect the generated many-to-many model named in the error. Done means the ORM reload completes without requiring a custom through table.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, node.js
Domain
backend, database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.