2015-11-18 3 views
8

ho il seguente modello in NodeJS con sequelize e un database MySQL:Incremento automatico id con sequelize in MySQL

var Sequelize = require('sequelize'); 
var User = sequelize.define('user', {   
     id: { 
      type: Sequelize.INTEGER, 
      autoIncrement: true, 
      primaryKey: true 
     }, 
     ... 
}; 

Sto cercando di aggiungere un nuovo utente al mio databse con il codice qui sotto:

sequelize.transaction().then(function(t) { 
     User.create({/* User data without id */}, { 
      transaction: t 
     }).then(function() { 
      t.commit(); 
     }).catch(function(error) { 
      t.rollback(); 
     }); 
    }); 

Dopo di che, io sono sempre l'errore successivo:

Executing (47f19f7b-a02d-4d72-ba7e-d5045520fffb): START TRANSACTION; 
Executing (47f19f7b-a02d-4d72-ba7e-d5045520fffb): SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ; 
Executing (47f19f7b-a02d-4d72-ba7e-d5045520fffb): SET autocommit = 1; 
Executing (47f19f7b-a02d-4d72-ba7e-d5045520fffb): INSERT INTO `user` (`id`, /* next fields */) VALUES (DEFAULT, /* next values */); 
Executing (47f19f7b-a02d-4d72-ba7e-d5045520fffb): ROLLBACK; 

E il messaggio di errore:

[SequelizeDatabaseError: ER_NO_DEFAULT_FOR_FIELD: Field 'id' doesn't have a default value] 
    name: 'SequelizeDatabaseError', 
    message: 'ER_NO_DEFAULT_FOR_FIELD: Field \'id\' doesn\'t have a default value' 

Tuttavia, se si imposta manualmente il valore id, funziona. Sembra che il sequelize stia cercando di impostare un valore predefinito nel campo id, invece di impostare un numero intero di autoincremento. Ho definito questo campo anche autoIncrement nel mio database.

Come posso fare questo inserimento? Devo impostare l'id manualmente?

EDIT

Questa è la mia definizione di tabella:

CREATE TABLE `user` (
    `id` int(11) NOT NULL AUTO_INCREMENT, 
    `uid` varchar(9) NOT NULL, 
    `name` varchar(20) NOT NULL, 
    `email` varchar(30) DEFAULT NULL, 
    `birthdate` date NOT NULL, 
    PRIMARY KEY (`id`), 
    UNIQUE KEY `uid_UNIQUE` (`uid`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8; 
+0

cosa fa la struttura del database simile? Forse non è impostato correttamente in MySQL stesso? – Andrius

+0

@Andrius, ho aggiornato la mia domanda per aggiungere questa informazione – Genzotto

risposta

9

Devi essere sicuro che non stai anche l'invio della chiave id a tutti.

Ho fatto un test di minimo rapido e sembrava funzionare molto:

var Sequelize = require('sequelize'); 
var sequelize = new Sequelize('cake3', 'root', 'root', { 
    define: { 
     timestamps: false 
    }, 
}); 
var User = sequelize.define('user1', {   
     id: { 
      type: Sequelize.INTEGER, 
      autoIncrement: true, 
      primaryKey: true 
     }, 
     name: { 
      type: Sequelize.STRING 
     } 
}); 

sequelize.transaction().then(function(t) { 
    User.create({name:'test'}, { 
     transaction: t 
    }).then(function() { 
     t.commit(); 
    }).catch(function(error) { 
     console.log(error); 
     t.rollback(); 
    }); 
}); 

Tabella discarica:

CREATE TABLE `user1s` (
    `id` int(11) NOT NULL, 
    `name` varchar(20) NOT NULL 
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8; 
ALTER TABLE `user1s` 
    ADD PRIMARY KEY (`id`); 
ALTER TABLE `user1s` 
    MODIFY `id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=1; 
+0

Il tuo codice mi ha aiutato a scoprire il mio problema. Nella mia definizione di tabella, ho impostato la colonna id come AUTO_INCREMENT, ma non ho impostato il suo valore iniziale (AUTO_INCREMENT = 1). Ora funziona in modo bello. Grazie mille! – Genzotto

+0

Hai controllato se hai NO_AUTO_VALUE_ON_ZERO? https://github.com/sequelize/sequelize/issues/5493 – igorsf