4

I'm new here !

I'm trying to do a query on a MS SQL Server2008 with sequelize, but I get this error:

Unhandled rejection SequelizeDatabaseError: Invalid column name 'id'. at Query.formatError (C:\xampp\htdocs\Lavori\Bit_Sense\API_BS\node_modules\s equelize\lib\dialects\mssql\query.js:217:10) at Request.userCallback (C:\xampp\htdocs\Lavori\Bit_Sense\API_BS\node_module s\sequelize\lib\dialects\mssql\query.js:66:25) at Request.callback (C:\xampp\htdocs\Lavori\Bit_Sense\API_BS\node_modules\te dious\lib\request.js:30:27) at Connection.STATE.SENT_CLIENT_REQUEST.events.message (C:\xampp\htdocs\Lavo ri\Bit_Sense\API_BS\node_modules\tedious\lib\connection.js:283:29) at Connection.dispatchEvent (C:\xampp\htdocs\Lavori\Bit_Sense\API_BS\node_mo dules\tedious\lib\connection.js:752:59) at MessageIO. (C:\xampp\htdocs\Lavori\Bit_Sense\API_BS\node_modul es\tedious\lib\connection.js:685:22) at MessageIO.emit (events.js:104:17) at MessageIO.eventData (C:\xampp\htdocs\Lavori\Bit_Sense\API_BS\node_modules \tedious\lib\message-io.js:58:21) at Socket. (C:\xampp\htdocs\Lavori\Bit_Sense\API_BS\node_modules\ tedious\lib\message-io.js:3:59) at Socket.emit (events.js:107:17) at readableAddChunk (_stream_readable.js:163:16) at Socket.Readable.push (_stream_readable.js:126:10) at TCP.onread (net.js:538:20)

I've installed this module: - sequelize ; - tedious ;

I haven't problem on the connection, only with this query:

db.KEY_ARTI.findAll({
  where:{
	      CACODICE: cacodice
	    }
	}).then(function(data) {
		res.send(data);
	});

What can I do ? I haven't ANY column called id

Here is my table:

var Sequelize = require('sequelize');
var settings = global.settings.databases.DATABASE;
var errors = global.errors;
var utilities = global.utilities; 

var sequelize = new Sequelize(settings.schema, settings.username, settings.password, {
      dialect: settings.dialect,
      host: settings.host,
	  port: settings.port, /* BISOGNA USARE LA DYNAMIC PORT */
      logging: function (str) {
          if(settings.log)
            console.log("querylog: "+str.replace("Executing (default):", "") );
      },
    });

sequelize.authenticate().then(function(err) {
    if (!!err) {
      console.log('Database '+settings.schema+' Connection Error:', err)
    } 
    else {
      console.log('Database '+settings.schema+' Connected')
    }
});

exports.sequelize = sequelize;

/*
 * KEY_ARTI
 */
exports.KEY_ARTI = sequelize.define('KEY_ARTI', {
	CACODICE: Sequelize.CHAR(20),
	CADESART: Sequelize.CHAR(40),
}

3
  • Can you post your initializing code as well? Commented Jul 23, 2015 at 16:48
  • here it's saying in Invalid column name 'id'. Can you check about this. Or post the code for review. Commented Jul 23, 2015 at 16:49
  • I added in the answer the code, and I'm sure that I have the Object, because if I do a console.log(db.KEY_ARTI) and isn't undefined or null Commented Jul 23, 2015 at 16:57

1 Answer 1

7

By default, sequelize will add a primary key called id, if you don't do anything yourself.

sequelize.define('model', {}); // Adds an id key

sequelize.define('model', {
  name: {
    primaryKey: true
    type: Sequelize.STRING
  }
}); // Doesn't add an id, because you already marked another column as primary key

It is also possible to have a model without a primary key, but I wouldn't recommend this

Sign up to request clarification or add additional context in comments.

1 Comment

Yes I find it ! I added the primary key, but, as you said, you can remove the attributes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.