I had an error in Sequelize with a foreign key not matching a column name. Checking the db, I saw that the column 'WokflowId' was named 'WorkflowID', so I changed the name in the Sequelize model.
It look like this
'use strict';
const Sequelize = require('sequelize');
const sequelize = require('../util/database');
const WorkflowsAccessRights = sequelize.define('Workflows_AccessRights', {
id:{
type: Sequelize.BIGINT,
autoIncrement: true,
allowNull: false,
primaryKey: true
},
WorkflowID: Sequelize.BIGINT,
GroupID: Sequelize.BIGINT,
Enabled: Sequelize.INTEGER
},{
timestamps: false,
freezeTableName: true,
});
module.exports = WorkflowsAccessRights
But now that I changed the column name to get the right one, I get this error
Column names in each table must be unique. Column name 'WorkflowId' in table 'Workflows_AccessRights' is specified more than once.
Except that no column has this name now... Did I miss something in the setup or is there a way to freeze the column name?
CodePudding user response:
Hope the following link will be helpful for you. It asks to put IF NOT EXISTS
to the SQL script as follows,
IF NOT EXISTS(SELECT * FROM sys.columns WHERE Name = 'ColumnName'
AND object_id = OBJECT_ID('YourTableName'))
BEGIN
ALTER TABLE dbo.YourTableName
ADD ColumnName INT -- or whatever it is
END
Check this link. It is the same question that you have asked
CodePudding user response:
So, after searching in the whole project. I found out that I was calling a foreignKey on WorkflowId. Therefore Sequelize tried to create a new column called WorkfklowId and had a bug doing it. I just renamed the foreignKey and it worked.