question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

MSSQL throws the error: "The column 'column' was specified multiple times for 'table'." when using `field` property in model definition

See original GitHub issue

What 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:closed
  • Created 7 years ago
  • Comments:11 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
renatoarghcommented, Nov 16, 2016

Just noticed that I can do this ugly workaround to prevent the error:

const options = {
  offset: 100,
  limit: 50,
  attributes: {
    exclude: ['SuperintendenciaId'] // here
  },
  include: [{
    model: ProcessoPessoa,
    as: 'pessoas',
    required: false,
    attributes: {
      exclude: ['ProcessoId'] // here
    },
  }]
}

The problema with this workaround is that I would have to remember to add this excludes throughout the whole project 😕

2reactions
jongearcommented, Dec 1, 2017

@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

Processo.belongsTo(Superintendencia, {
  as: 'superintendencia',
  foreignKey: 'SuperintendenciaId'
})

should be

Processo.belongsTo(Superintendencia, {
  as: 'superintendencia',
  foreignKey: 'superintendenciaId'
})

Original Processo snippet

Processo.hasMany(ProcessoPessoa, {
  as: 'pessoas',
  foreignKey: 'ProcessoId'
})

should be

Processo.hasMany(ProcessoPessoa, {
  as: 'pessoas',
  foreignKey: 'processoId'
})
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found