Reputation: 33
I'm starting to play around with node express and Sequelize. I follow the example of Sequelize/express
from them git page, but I'm having a problem when I execute de app. All the tables are created right and shows no errors, but when I look at the tables instead of creating a foreign key named secretariaid
to reference the secretaria table, its creating a field named secretarumid
with throws and error when trying to use the database.
I'm a bit confuse about were is the error:
models and migrations created using sequelize model:create.
oficina.js
'use strict';
module.exports = function(sequelize, DataTypes) {
var Oficina = sequelize.define('Oficina', {
nombre_oficina: DataTypes.STRING,
direccion: DataTypes.STRING,
telefono: DataTypes.STRING,
interno: DataTypes.STRING,
email: DataTypes.STRING
}, {
classMethods: {
associate: function(models) {
Oficina.belongsTo(models.Secretaria, {
onDelete: "CASCADE",
foreignKey: {
allowNull: false
}
});
}
}
});
return Oficina;
};
secretaria.js
'use strict';
module.exports = function(sequelize, DataTypes) {
var Secretaria = sequelize.define('Secretaria', {
nombre_secretaria: DataTypes.STRING
}, {
classMethods: {
associate: function(models) {
Secretaria.hasMany(models.Oficina);
}
}
});
return Secretaria;
};
sync() on www.js
models.sequelize.sync().then(function() {
/**
* Listen on provided port, on all network interfaces.
*/
server.listen(port);
server.on('error', onError);
server.on('listening', onListening);
});
It seams that the name is the problem, if instead of "Secretaria" i use "Secre" it works fine, but "Secret" or "Secret" + any other letter does not pluralize the table secretaria and the foreign key end up "secretariumId". Is "Secret+++++" some kind of reserved word ?
UPDATE
"use strict";
module.exports = function(sequelize, DataTypes) {
var Secretaria = sequelize.define("Secretaria",
{
nombre: DataTypes.STRING
},
{
classMethods: {
associate: function(models) {
Secretaria.hasMany(models.Oficina)
}
},
name: {
singular: 'secretaria',
plural: 'secretarias',
},
tableName: "secretarias"
});
return Secretaria;
};
Using the options name fixes the foreign key problem, and tableName is fixing the pluralization. Now everything is working fine.
Upvotes: 3
Views: 1385
Reputation: 27599
This looks to be a language issue, and I bet the underlying table name is incorrect as well as Sequelize uses the plural by default. Sequelize is in English and uses the inflection library to figure out the singular/plural names for Models, as described in the Naming Strategy in the documentation. Since it looks like you are using Spanish, it is incorrectly pluralizing the name of the Model. To get around this you can specify the values for the singular and plural name in the Model definition, or use English names :P
See details for options.name
under the Sequelize.define
() documentation.
You can also choose if you want the database table name to be plural (default/false
) or singular (true
) by setting freezeTableName: true/false,
. This option is really using the exact value that you used for the first argument - it will maintain case, etc. - but also serves the purpose to make it singular in your case.
var Secretaria = sequelize.define('Secretaria', {
nombre_secretaria: DataTypes.STRING
},
name: {
singular: 'secretaria',
plural: 'secretarias',
},
freezeTableName: true, // table name is singular "Secreteria"
Upvotes: 1