Using the following model file in conjunction with Sequelize, this code runs without error and allows me to perform an insert:
var crypto = require('crypto');
module.exports = function(sequelize, DataTypes) {
var User = sequelize.define('User',
{
title: DataTypes.STRING,
name: DataTypes.STRING,
email: DataTypes.STRING,
username: DataTypes.STRING,
hashedPassword: DataTypes.STRING,
provider: DataTypes.STRING,
salt: DataTypes.STRING,
facebookUserId: DataTypes.INTEGER,
twitterUserId: DataTypes.INTEGER,
twitterKey: DataTypes.STRING,
twitterSecret: DataTypes.STRING,
github: DataTypes.STRING,
openID: DataTypes.STRING
},
...
)
However, when I try to manually add my own fields of interest, i.e. adding a ZIP code field as below:
module.exports = function(sequelize, DataTypes) {
var User = sequelize.define('User',
{
title: DataTypes.STRING,
name: DataTypes.STRING,
email: DataTypes.STRING,
username: DataTypes.STRING,
hashedPassword: DataTypes.STRING,
provider: DataTypes.STRING,
salt: DataTypes.STRING,
facebookUserId: DataTypes.INTEGER,
twitterUserId: DataTypes.INTEGER,
twitterKey: DataTypes.STRING,
twitterSecret: DataTypes.STRING,
github: DataTypes.STRING,
openID: DataTypes.STRING,
ZIP: DataTypes.INT
},
the following error propagates:
SequelizeDatabaseError: ER_BAD_FIELD_ERROR: Unknown column 'title' in 'field list'
Why am I not allowed to add fields within the models to be able to access them from the controller?
Maybe sequelize/SQL is confused because of an existing row in an old table. Have you tried deleting any existing tables in the db if you've already made some? As it was said already, syncing the db is important, but also set "force" to true so that you wipe the old tables (ONLY DO THIS IF YOUR DB ISN'T IN PRODUCTION!). It's in the docs: http://docs.sequelizejs.com/en/1.7.0/articles/getting-started/
It worked for me.
sequelize
.sync({ force: true })
.then(function(err) {
console.log('It worked!');
}, function (err) {
console.log('An error occurred while creating the table:', err);
});