Create the associated model of fixture if it doesn't exist.#47
Create the associated model of fixture if it doesn't exist.#47jasonku wants to merge 1 commit intodomasx2:masterfrom
Conversation
* Sequelize supports creating of associated models in one step. See: docs.sequelizejs.com/en/latest/docs/associations/#creating-with-associations * If the associated model of a fixture doesn't exist in the database yet, pass along the data of the associated model so that it's included in `Model.build` and ends up getting created.
|
I kinda feel in a curmudgeony-coservative-asshat sort of way that associated models are better created explicitly |
|
This PR doesn't take away the ability to explicitly create associated models in separate calls. It just adds the ability to create the associated model in one call. |
|
If this provides the option to create associations on the fly using nesting, and it causes no backwards incompatibility, then this would be an amazing feature! |
|
@domasx2 Any plans to either merge or close this? This change is backwards compatible. |
| result[assoc.identifier] = obj[assoc.target.primaryKeyField || 'id']; | ||
| if (obj) { | ||
| result[assoc.identifier] = obj[assoc.target.primaryKeyField || 'id']; | ||
| } else { |
There was a problem hiding this comment.
Can we add a check here to ensure that on-the-fly association creation is what is intended? Actually this area of code currently has bad error reporting.
I suggest we add a check if the option was specified, if not, it should error and say association not found , such:
throw new Error('No associated model found for: \n\t' + JSON.stringify(options) + '\nwhile processing fixture: \n\t' + JSON.stringify(data));
I'm not positive about the logic to check if your include option was specified, could you add it along with this fallback error case?
docs.sequelizejs.com/en/latest/docs/associations/#creating-with-associations
yet, pass along the data of the associated model so that it's included
in
Model.buildand ends up getting created.