MSSQL throws the error: "The column 'column' was specified multiple times for 'table'." when using `field` property in model definition
See original GitHub issueWhat you are doing?
I am using MSSQL and I am trying to perform a .findAll
with limit
and offset
with a model that has two associations (one hasMany and one belongsTo), I am trying to include the hasMany
associations.
My model definition uses the fields
property to map column names.
Above is the full code to reproduce the issue
const Sequelize = require('sequelize')
const DataTypes = Sequelize.DataTypes
const sequelize = new Sequelize(process.env.CONNECTION_STRING, {
timezone: 'America/Sao_Paulo',
logging: console.log,
dialect: 'mssql',
dialectOptions: {
encrypt: true
},
define: {
charset: 'utf8',
collation: 'utf8_general_ci'
}
})
const ProcessoPessoa = sequelize.define('ProcessoPessoa', {
id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
field: 'Id'
},
processoId: {
type: DataTypes.STRING,
allowNull: false,
field: 'ProcessoId'
}
}, {
freezeTableName: true,
tableName: 'ProcessosPessoas',
createdAt: false,
updatedAt: false
})
const Superintendencia = sequelize.define('Superintendencia', {
id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
field: 'Id'
},
estadoId: {
type: DataTypes.INTEGER,
allowNull: false,
references: {
model: 'Estados',
key: 'Id'
},
field: 'EstadoId'
},
nome: {
type: DataTypes.STRING,
allowNull: false,
field: 'Nome'
}
}, {
freezeTableName: true,
tableName: 'Superintendencias',
createdAt: false,
updatedAt: false
})
const Processo = sequelize.define('Processo', {
id: {
type: DataTypes.STRING,
allowNull: false,
primaryKey: true,
field: 'Id'
},
superintendenciaId: {
type: DataTypes.INTEGER,
allowNull: true,
field: 'SuperintendenciaId'
}
}, {
freezeTableName: true,
tableName: 'Processos',
createdAt: false,
updatedAt: false
})
Processo.hasMany(ProcessoPessoa, {
as: 'pessoas',
foreignKey: 'ProcessoId'
})
// If I comment the following 4 lines that define the belongsTo association
// the problem go away (but of course, I create another problem)
Processo.belongsTo(Superintendencia, {
as: 'superintendencia',
foreignKey: 'SuperintendenciaId'
})
const options = {
offset: 100,
limit: 50,
include: [{
model: ProcessoPessoa,
as: 'pessoas',
required: false
}]
}
Processo.findAll(options).then(function (results) {
// Will never get here - MSSQL will throw an error
console.log(JSON.stringify(results, null, 4))
})
What do you expect to happen?
I expected that the query would work
What is actually happening?
MSSQL is throwing the following error: The column 'SuperintendenciaId' was specified multiple times for 'Processo'.
due to duplicated columns in the select
that is generated as follows:
Output:
SELECT
[Processo].*,
[pessoas].[Id] AS [pessoas.id],
[pessoas].[ProcessoId] AS [pessoas.processoId],
[pessoas].[ProcessoId] AS [pessoas.ProcessoId] -- I think this is also not needed (though not part of the problem)
FROM (
SELECT
[Processo].[Id] AS [id],
[Processo].[SuperintendenciaId] AS [superintendenciaId],
[Processo].[SuperintendenciaId] -- This line is not expected and should not be created
FROM
[Processos] AS [Processo]
ORDER BY
[Id]
OFFSET 100 ROWS FETCH NEXT 50 ROWS ONLY
) AS [Processo]
LEFT OUTER JOIN
[ProcessosPessoas] AS [pessoas] ON
[Processo].[id] = [pessoas].[ProcessoId];
Note that when I remove the field
property from my Models then it works great.
Dialect: mssql Database version: MSSQL 12.0.2000.8 Sequelize version: 3.25.0
Issue Analytics
- State:
- Created 7 years ago
- Comments:11 (2 by maintainers)
Top Results From Across the Web
SQL 2005 - The column was specified multiple times
Solution: don't use * in the subquery, but explicitly select each column you wish to see, aliasing any column name that appears more...
Read more >The column was specified multiple times - DBA Stack Exchange
The column 'item_code' was specified multiple times for 'tt'. I am using mssql 2012. I am quite naive to SQL . Thanks in...
Read more >Database Engine events and errors - SQL Server
ls" is specified multiple times in the column list of the UNPIVOT operator. 278, 16, No, The text, ntext, and image data types...
Read more >SQL error messages and exceptions - Oracle Help Center
In an ALTER TABLE statement, the column ' <columnName> ' has been specified as NOT NULL and either the DEFAULT clause was not...
Read more >Microsoft SQL : The column 'Name' was specified mu...
I just got this error when Appending two tables together where the columns had the same name but in different case. One was...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Just noticed that I can do this ugly workaround to prevent the error:
The problema with this workaround is that I would have to remember to add this
excludes
throughout the whole project 😕@renatoargh so I actually figured this one out. For your foreign key relationship you must use the exact casing used in the relationship in sequelize NOT the casing used in the database.
Original Processo snippet
should be
Original Processo snippet
should be