Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sequelize migration not working

I created a migration and ran it. It says it worked fine, but nothing happened. I don't think it is even connecting to my database.

My Migration file:

var util = require("util");
module.exports = {
up : function(migration, DataTypes, done) {
    
migration.createTable('nameOfTheNewTable', {
    attr1 : DataTypes.STRING,
    attr2 : DataTypes.INTEGER,
    attr3 : {
        type : DataTypes.BOOLEAN,
        defaultValue : false,
        allowNull : false
    }
}).success(
        function() {

            migration.describeTable('nameOfTheNewTable').success(
                    function(attributes) {
                        util.puts("nameOfTheNewTable Schema: "
                                + JSON.stringify(attributes));
                        done();
                    });

        });
},
down : function(migration, DataTypes, done) {
    // logic for reverting the changes
}
};

My Config.json:

{
  "development": {
    "username": "user",
    "password": "pw",
    "database": "my-db",
    "dialect" : "sqlite",
    "host": "localhost"
  }
}

The command:

./node_modules/sequelize/bin/sequelize --migrate --env development
Loaded configuration file "config/config.json".
Using environment "development".
Running migrations...
20130921234513-initial.js
nameOfTheNewTable Schema: {"attr1":{"type":"VARCHAR(255)","allowNull":true,"defaultValue":null},"attr2":{"type":"INTEGER","allowNull":true,"defaultValue":null},"attr3":{"type":"TINYINT(1)","allowNull":false,"defaultValue":false}}
Completed in 8ms

I can run this over and over and the output is always the same. I've tried it on a database which I know to have existing tables and try to describe those tables and still nothing happens.

Am I doing something wrong?

EDIT:

I'm pretty sure I'm not connecting to the db, but try as I might I cannot connect using the migration. I can connect using sqlite3 my-db.sqlite and run commands such as .tables to see tables I have created previously, but I cannot for the life of me get the "nameOfTheNewTable" table created using a migration. (I want to create indexes in the migration too). I have tried using "development", changing values in the config.json like the host, database (my-db, ../my-db, my-db.sqlite), etc.

Here's a good example, in the config.json I put "database" : "bad-db" and the output from the migration is exactly the same. When it is done, there is no bad-db.sqlite file to be found.

like image 228
Jess Avatar asked Sep 23 '13 16:09

Jess


People also ask

How do I update a column in Sequelize migration?

Note: Take note that queryInterface. createColumn from our initial migration file or the update migration file queryInterface. changeColumn has change to queryInterface. addColumn in order for us to add new column to our database table.

How do I run migration in node JS?

Creating Migrations To create a migration, execute db-migrate create with a title. node-db-migrate will create a node module within ./migrations/ which contains the following two exports: exports. up = function (db, callback) { callback(); }; exports.


2 Answers

You need to specify the 'storage' parameter in your config.json, so that sequelize knows what file to use as the sqlite DB.

Sequelize defaults to using memory storage for sqlite, so it's migrating an in-memory database, then exiting, effectively destroying the DB it just migrated.

like image 147
Elliot Foster Avatar answered Sep 19 '22 13:09

Elliot Foster


you most likely have to wait for migration.createTable to finish:

migration.createTable(/*your options*/).success(function() {  
    migration.describeTable('nameOfTheNewTable').success(function(attributes) {  
        util.puts("nameOfTheNewTable Schema: " + JSON.stringify(attributes));  
        done()  
    });
})
like image 22
sdepold Avatar answered Sep 20 '22 13:09

sdepold