Sequelize Many to Many Relationship using Through does not insert additional attributes












0















I have a many to many relationship between: Step and Control Through ControlsConfig.
When creating a Control object and call addStep function and specify the additional attributes (which exist in the relation table), Sequelize creates the records in the relational table ControlsConfig but the additional attributes are NULLs.



PS: The tables are creating correctly in the database.




Table 1: Step



Table 2: Control



Relation table: ControlsConfig




Step



 var Step = sequelize.define('Step', {
title: { type: DataTypes.STRING, allowNull: false },
description: DataTypes.STRING,
type: { type: DataTypes.ENUM('task', 'approval'), allowNull: false, defaultValue: 'task' },
order: DataTypes.INTEGER
});

Step.associate = function(models) {
models.Step.belongsTo(models.User);
models.Step.belongsTo(models.Template);
models.Step.hasMany(models.Action);
};


Control



var Control = sequelize.define('Control', {
label: { type: DataTypes.STRING, allowNull: false },
order: { type: DataTypes.INTEGER },
type: { type: DataTypes.ENUM('text', 'yes/no') },
config: { type: DataTypes.TEXT },
controlUiId: { type: DataTypes.STRING }
});

Control.associate = function(models) {
models.Control.belongsTo(models.Section);
};


ControlsConfigs



  module.exports = (sequelize, DataTypes) => {
var ControlsConfig = sequelize.define('ControlsConfig', {
visibility: { type: DataTypes.ENUM('hidden', 'readonly', 'editable', 'required') },
config: { type: DataTypes.TEXT }
});

ControlsConfig.associate = function(models) {
models.Control.belongsToMany(models.Step, { through: models.ControlsConfig });
models.Step.belongsToMany(models.Control, { through: models.ControlsConfig });
models.ControlsConfig.belongsTo(models.Template);
};

return ControlsConfig;
};


Insertion:



try {
var step1 = await Step.create({ /*bla bla*/ });
var control1 = await Control.create({ /*bla bla*/ });
var OK = await control1.addStep(step1, {through: { config: 'THIS FIELD ALWAYS APPEARS NULL' }});
} catch (error) { /* No errors*/ }


I am following the same strategy stated at the documentation



//If you want additional attributes in your join table, you can define a model for the join table in sequelize, before you define the association, and then tell sequelize that it should use that model for joining, instead of creating a new one:

const User = sequelize.define('user', {})
const Project = sequelize.define('project', {})
const UserProjects = sequelize.define('userProjects', {
status: DataTypes.STRING
})

User.belongsToMany(Project, { through: UserProjects })
Project.belongsToMany(User, { through: UserProjects })

//To add a new project to a user and set its status, you pass extra options.through to the setter, which contains the attributes for the join table

user.addProject(project, { through: { status: 'started' }})









share|improve this question

























  • /*bla bla*/ is actually the important part here. Did you even declare any of the schema attributes with NOT NULL? If you are unsure what that means, then you should actually include the schema definition you are omitting from the question.

    – Neil Lunn
    Nov 24 '18 at 23:50













  • Thanks @NeilLunn , I thought the attributes are irrelevant, I have modified the question and added the previously omitted code.

    – Ahmed Elazazy
    Nov 25 '18 at 0:33













  • Well if the additional attributes are NULL then this is because you have not supplied any other values and did not explicitly forbid it in your schema definition above. SQL based RDMS do not just magically exclude table columns when you omit the data. You either supply it, set defaults or explicitly forbid it to force your code implementation to supply values.

    – Neil Lunn
    Nov 25 '18 at 0:37













  • @NeilLunn , Doesn't this line mean setting the value of "config" column which is one of the additional attributes but it becomes always null in the database? var OK = await control1.addStep(step1, {through: { config: 'THIS FIELD ALWAYS APPEARS NULL' }});

    – Ahmed Elazazy
    Nov 25 '18 at 0:41
















0















I have a many to many relationship between: Step and Control Through ControlsConfig.
When creating a Control object and call addStep function and specify the additional attributes (which exist in the relation table), Sequelize creates the records in the relational table ControlsConfig but the additional attributes are NULLs.



PS: The tables are creating correctly in the database.




Table 1: Step



Table 2: Control



Relation table: ControlsConfig




Step



 var Step = sequelize.define('Step', {
title: { type: DataTypes.STRING, allowNull: false },
description: DataTypes.STRING,
type: { type: DataTypes.ENUM('task', 'approval'), allowNull: false, defaultValue: 'task' },
order: DataTypes.INTEGER
});

Step.associate = function(models) {
models.Step.belongsTo(models.User);
models.Step.belongsTo(models.Template);
models.Step.hasMany(models.Action);
};


Control



var Control = sequelize.define('Control', {
label: { type: DataTypes.STRING, allowNull: false },
order: { type: DataTypes.INTEGER },
type: { type: DataTypes.ENUM('text', 'yes/no') },
config: { type: DataTypes.TEXT },
controlUiId: { type: DataTypes.STRING }
});

Control.associate = function(models) {
models.Control.belongsTo(models.Section);
};


ControlsConfigs



  module.exports = (sequelize, DataTypes) => {
var ControlsConfig = sequelize.define('ControlsConfig', {
visibility: { type: DataTypes.ENUM('hidden', 'readonly', 'editable', 'required') },
config: { type: DataTypes.TEXT }
});

ControlsConfig.associate = function(models) {
models.Control.belongsToMany(models.Step, { through: models.ControlsConfig });
models.Step.belongsToMany(models.Control, { through: models.ControlsConfig });
models.ControlsConfig.belongsTo(models.Template);
};

return ControlsConfig;
};


Insertion:



try {
var step1 = await Step.create({ /*bla bla*/ });
var control1 = await Control.create({ /*bla bla*/ });
var OK = await control1.addStep(step1, {through: { config: 'THIS FIELD ALWAYS APPEARS NULL' }});
} catch (error) { /* No errors*/ }


I am following the same strategy stated at the documentation



//If you want additional attributes in your join table, you can define a model for the join table in sequelize, before you define the association, and then tell sequelize that it should use that model for joining, instead of creating a new one:

const User = sequelize.define('user', {})
const Project = sequelize.define('project', {})
const UserProjects = sequelize.define('userProjects', {
status: DataTypes.STRING
})

User.belongsToMany(Project, { through: UserProjects })
Project.belongsToMany(User, { through: UserProjects })

//To add a new project to a user and set its status, you pass extra options.through to the setter, which contains the attributes for the join table

user.addProject(project, { through: { status: 'started' }})









share|improve this question

























  • /*bla bla*/ is actually the important part here. Did you even declare any of the schema attributes with NOT NULL? If you are unsure what that means, then you should actually include the schema definition you are omitting from the question.

    – Neil Lunn
    Nov 24 '18 at 23:50













  • Thanks @NeilLunn , I thought the attributes are irrelevant, I have modified the question and added the previously omitted code.

    – Ahmed Elazazy
    Nov 25 '18 at 0:33













  • Well if the additional attributes are NULL then this is because you have not supplied any other values and did not explicitly forbid it in your schema definition above. SQL based RDMS do not just magically exclude table columns when you omit the data. You either supply it, set defaults or explicitly forbid it to force your code implementation to supply values.

    – Neil Lunn
    Nov 25 '18 at 0:37













  • @NeilLunn , Doesn't this line mean setting the value of "config" column which is one of the additional attributes but it becomes always null in the database? var OK = await control1.addStep(step1, {through: { config: 'THIS FIELD ALWAYS APPEARS NULL' }});

    – Ahmed Elazazy
    Nov 25 '18 at 0:41














0












0








0








I have a many to many relationship between: Step and Control Through ControlsConfig.
When creating a Control object and call addStep function and specify the additional attributes (which exist in the relation table), Sequelize creates the records in the relational table ControlsConfig but the additional attributes are NULLs.



PS: The tables are creating correctly in the database.




Table 1: Step



Table 2: Control



Relation table: ControlsConfig




Step



 var Step = sequelize.define('Step', {
title: { type: DataTypes.STRING, allowNull: false },
description: DataTypes.STRING,
type: { type: DataTypes.ENUM('task', 'approval'), allowNull: false, defaultValue: 'task' },
order: DataTypes.INTEGER
});

Step.associate = function(models) {
models.Step.belongsTo(models.User);
models.Step.belongsTo(models.Template);
models.Step.hasMany(models.Action);
};


Control



var Control = sequelize.define('Control', {
label: { type: DataTypes.STRING, allowNull: false },
order: { type: DataTypes.INTEGER },
type: { type: DataTypes.ENUM('text', 'yes/no') },
config: { type: DataTypes.TEXT },
controlUiId: { type: DataTypes.STRING }
});

Control.associate = function(models) {
models.Control.belongsTo(models.Section);
};


ControlsConfigs



  module.exports = (sequelize, DataTypes) => {
var ControlsConfig = sequelize.define('ControlsConfig', {
visibility: { type: DataTypes.ENUM('hidden', 'readonly', 'editable', 'required') },
config: { type: DataTypes.TEXT }
});

ControlsConfig.associate = function(models) {
models.Control.belongsToMany(models.Step, { through: models.ControlsConfig });
models.Step.belongsToMany(models.Control, { through: models.ControlsConfig });
models.ControlsConfig.belongsTo(models.Template);
};

return ControlsConfig;
};


Insertion:



try {
var step1 = await Step.create({ /*bla bla*/ });
var control1 = await Control.create({ /*bla bla*/ });
var OK = await control1.addStep(step1, {through: { config: 'THIS FIELD ALWAYS APPEARS NULL' }});
} catch (error) { /* No errors*/ }


I am following the same strategy stated at the documentation



//If you want additional attributes in your join table, you can define a model for the join table in sequelize, before you define the association, and then tell sequelize that it should use that model for joining, instead of creating a new one:

const User = sequelize.define('user', {})
const Project = sequelize.define('project', {})
const UserProjects = sequelize.define('userProjects', {
status: DataTypes.STRING
})

User.belongsToMany(Project, { through: UserProjects })
Project.belongsToMany(User, { through: UserProjects })

//To add a new project to a user and set its status, you pass extra options.through to the setter, which contains the attributes for the join table

user.addProject(project, { through: { status: 'started' }})









share|improve this question
















I have a many to many relationship between: Step and Control Through ControlsConfig.
When creating a Control object and call addStep function and specify the additional attributes (which exist in the relation table), Sequelize creates the records in the relational table ControlsConfig but the additional attributes are NULLs.



PS: The tables are creating correctly in the database.




Table 1: Step



Table 2: Control



Relation table: ControlsConfig




Step



 var Step = sequelize.define('Step', {
title: { type: DataTypes.STRING, allowNull: false },
description: DataTypes.STRING,
type: { type: DataTypes.ENUM('task', 'approval'), allowNull: false, defaultValue: 'task' },
order: DataTypes.INTEGER
});

Step.associate = function(models) {
models.Step.belongsTo(models.User);
models.Step.belongsTo(models.Template);
models.Step.hasMany(models.Action);
};


Control



var Control = sequelize.define('Control', {
label: { type: DataTypes.STRING, allowNull: false },
order: { type: DataTypes.INTEGER },
type: { type: DataTypes.ENUM('text', 'yes/no') },
config: { type: DataTypes.TEXT },
controlUiId: { type: DataTypes.STRING }
});

Control.associate = function(models) {
models.Control.belongsTo(models.Section);
};


ControlsConfigs



  module.exports = (sequelize, DataTypes) => {
var ControlsConfig = sequelize.define('ControlsConfig', {
visibility: { type: DataTypes.ENUM('hidden', 'readonly', 'editable', 'required') },
config: { type: DataTypes.TEXT }
});

ControlsConfig.associate = function(models) {
models.Control.belongsToMany(models.Step, { through: models.ControlsConfig });
models.Step.belongsToMany(models.Control, { through: models.ControlsConfig });
models.ControlsConfig.belongsTo(models.Template);
};

return ControlsConfig;
};


Insertion:



try {
var step1 = await Step.create({ /*bla bla*/ });
var control1 = await Control.create({ /*bla bla*/ });
var OK = await control1.addStep(step1, {through: { config: 'THIS FIELD ALWAYS APPEARS NULL' }});
} catch (error) { /* No errors*/ }


I am following the same strategy stated at the documentation



//If you want additional attributes in your join table, you can define a model for the join table in sequelize, before you define the association, and then tell sequelize that it should use that model for joining, instead of creating a new one:

const User = sequelize.define('user', {})
const Project = sequelize.define('project', {})
const UserProjects = sequelize.define('userProjects', {
status: DataTypes.STRING
})

User.belongsToMany(Project, { through: UserProjects })
Project.belongsToMany(User, { through: UserProjects })

//To add a new project to a user and set its status, you pass extra options.through to the setter, which contains the attributes for the join table

user.addProject(project, { through: { status: 'started' }})






sql node.js sequelize.js






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 0:32







Ahmed Elazazy

















asked Nov 24 '18 at 23:01









Ahmed ElazazyAhmed Elazazy

1117




1117













  • /*bla bla*/ is actually the important part here. Did you even declare any of the schema attributes with NOT NULL? If you are unsure what that means, then you should actually include the schema definition you are omitting from the question.

    – Neil Lunn
    Nov 24 '18 at 23:50













  • Thanks @NeilLunn , I thought the attributes are irrelevant, I have modified the question and added the previously omitted code.

    – Ahmed Elazazy
    Nov 25 '18 at 0:33













  • Well if the additional attributes are NULL then this is because you have not supplied any other values and did not explicitly forbid it in your schema definition above. SQL based RDMS do not just magically exclude table columns when you omit the data. You either supply it, set defaults or explicitly forbid it to force your code implementation to supply values.

    – Neil Lunn
    Nov 25 '18 at 0:37













  • @NeilLunn , Doesn't this line mean setting the value of "config" column which is one of the additional attributes but it becomes always null in the database? var OK = await control1.addStep(step1, {through: { config: 'THIS FIELD ALWAYS APPEARS NULL' }});

    – Ahmed Elazazy
    Nov 25 '18 at 0:41



















  • /*bla bla*/ is actually the important part here. Did you even declare any of the schema attributes with NOT NULL? If you are unsure what that means, then you should actually include the schema definition you are omitting from the question.

    – Neil Lunn
    Nov 24 '18 at 23:50













  • Thanks @NeilLunn , I thought the attributes are irrelevant, I have modified the question and added the previously omitted code.

    – Ahmed Elazazy
    Nov 25 '18 at 0:33













  • Well if the additional attributes are NULL then this is because you have not supplied any other values and did not explicitly forbid it in your schema definition above. SQL based RDMS do not just magically exclude table columns when you omit the data. You either supply it, set defaults or explicitly forbid it to force your code implementation to supply values.

    – Neil Lunn
    Nov 25 '18 at 0:37













  • @NeilLunn , Doesn't this line mean setting the value of "config" column which is one of the additional attributes but it becomes always null in the database? var OK = await control1.addStep(step1, {through: { config: 'THIS FIELD ALWAYS APPEARS NULL' }});

    – Ahmed Elazazy
    Nov 25 '18 at 0:41

















/*bla bla*/ is actually the important part here. Did you even declare any of the schema attributes with NOT NULL? If you are unsure what that means, then you should actually include the schema definition you are omitting from the question.

– Neil Lunn
Nov 24 '18 at 23:50







/*bla bla*/ is actually the important part here. Did you even declare any of the schema attributes with NOT NULL? If you are unsure what that means, then you should actually include the schema definition you are omitting from the question.

– Neil Lunn
Nov 24 '18 at 23:50















Thanks @NeilLunn , I thought the attributes are irrelevant, I have modified the question and added the previously omitted code.

– Ahmed Elazazy
Nov 25 '18 at 0:33







Thanks @NeilLunn , I thought the attributes are irrelevant, I have modified the question and added the previously omitted code.

– Ahmed Elazazy
Nov 25 '18 at 0:33















Well if the additional attributes are NULL then this is because you have not supplied any other values and did not explicitly forbid it in your schema definition above. SQL based RDMS do not just magically exclude table columns when you omit the data. You either supply it, set defaults or explicitly forbid it to force your code implementation to supply values.

– Neil Lunn
Nov 25 '18 at 0:37







Well if the additional attributes are NULL then this is because you have not supplied any other values and did not explicitly forbid it in your schema definition above. SQL based RDMS do not just magically exclude table columns when you omit the data. You either supply it, set defaults or explicitly forbid it to force your code implementation to supply values.

– Neil Lunn
Nov 25 '18 at 0:37















@NeilLunn , Doesn't this line mean setting the value of "config" column which is one of the additional attributes but it becomes always null in the database? var OK = await control1.addStep(step1, {through: { config: 'THIS FIELD ALWAYS APPEARS NULL' }});

– Ahmed Elazazy
Nov 25 '18 at 0:41





@NeilLunn , Doesn't this line mean setting the value of "config" column which is one of the additional attributes but it becomes always null in the database? var OK = await control1.addStep(step1, {through: { config: 'THIS FIELD ALWAYS APPEARS NULL' }});

– Ahmed Elazazy
Nov 25 '18 at 0:41












1 Answer
1






active

oldest

votes


















0














You have to pass edit: true to the addProject and addStep method.



See this answer it has a similar issue



Sequelize belongsToMany additional attributes in join table






share|improve this answer
























  • At the mentioned question, the additional attribute was for a column named "edit" that is why it is passed to the through object, but in my case, it is named "config" which did not work when passing it.

    – Ahmed Elazazy
    Nov 25 '18 at 21:19











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53463125%2fsequelize-many-to-many-relationship-using-through-does-not-insert-additional-att%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You have to pass edit: true to the addProject and addStep method.



See this answer it has a similar issue



Sequelize belongsToMany additional attributes in join table






share|improve this answer
























  • At the mentioned question, the additional attribute was for a column named "edit" that is why it is passed to the through object, but in my case, it is named "config" which did not work when passing it.

    – Ahmed Elazazy
    Nov 25 '18 at 21:19
















0














You have to pass edit: true to the addProject and addStep method.



See this answer it has a similar issue



Sequelize belongsToMany additional attributes in join table






share|improve this answer
























  • At the mentioned question, the additional attribute was for a column named "edit" that is why it is passed to the through object, but in my case, it is named "config" which did not work when passing it.

    – Ahmed Elazazy
    Nov 25 '18 at 21:19














0












0








0







You have to pass edit: true to the addProject and addStep method.



See this answer it has a similar issue



Sequelize belongsToMany additional attributes in join table






share|improve this answer













You have to pass edit: true to the addProject and addStep method.



See this answer it has a similar issue



Sequelize belongsToMany additional attributes in join table







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 25 '18 at 11:04









Rahul SharmaRahul Sharma

828717




828717













  • At the mentioned question, the additional attribute was for a column named "edit" that is why it is passed to the through object, but in my case, it is named "config" which did not work when passing it.

    – Ahmed Elazazy
    Nov 25 '18 at 21:19



















  • At the mentioned question, the additional attribute was for a column named "edit" that is why it is passed to the through object, but in my case, it is named "config" which did not work when passing it.

    – Ahmed Elazazy
    Nov 25 '18 at 21:19

















At the mentioned question, the additional attribute was for a column named "edit" that is why it is passed to the through object, but in my case, it is named "config" which did not work when passing it.

– Ahmed Elazazy
Nov 25 '18 at 21:19





At the mentioned question, the additional attribute was for a column named "edit" that is why it is passed to the through object, but in my case, it is named "config" which did not work when passing it.

– Ahmed Elazazy
Nov 25 '18 at 21:19




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53463125%2fsequelize-many-to-many-relationship-using-through-does-not-insert-additional-att%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'