Himmators Himmators - 5 months ago 15
Node.js Question

belongsTo doesn't do anything

var Sequelize = require('sequelize');
var sequelize = new Sequelize('database', 'username', 'password', {
host: 'localhost',
dialect: 'sqlite',

// SQLite only
storage: 'db.sqlite'
});

var Driver = sequelize.define('driver', {
auth: Sequelize.STRING,
name: Sequelize.STRING,
phone: Sequelize.INTEGER,
bank: Sequelize.INTEGER,
idNo: Sequelize.STRING
});

var Hail = sequelize.define('hail', {
lat: Sequelize.INTEGER,
lon: Sequelize.INTEGER,
})

Hail.belongsTo(Driver);

sequelize.sync().then(function() {
return Hail.create({
lat: 123,
lon: 1234
});
}).then(function(driver) {
console.log(driver.get({
plain: true
}));
});


I'm trying to use the belongs to method to connect a hail to a driver. It doesn't seem to do anything. The log doesn't output any belongsTo field and hail doesn't have a driverID column in the database. I don't get any errors.

Answer

I suspect you've synced the database before adding the belongsTo call. The "sync" operation by default would not recreate the tables or modify the existing tables. You need to sync in a "force" manner once:

sequelize.sync({force: true}).then(function() {
    // ...
});

Note that this would destroy all the existing records you have in the tables.

Or, look into setting up the migrations.