【问题标题】:sequelize migration not working续集迁移不起作用
【发布时间】:2013-11-10 09:05:54
【问题描述】:

我创建了一个迁移并运行它。它说它工作正常,但什么也没发生。我认为它甚至没有连接到我的数据库。

我的迁移文件:

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
}
};

我的 Config.json:

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

命令:

./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

我可以一遍又一遍地运行它,并且输出总是相同的。我已经在我知道有现有表的数据库上进行了尝试,并尝试描述这些表,但仍然没有任何反应。

我是不是做错了什么?

编辑:

我很确定我没有连接到数据库,但请尝试我可能无法使用迁移连接。我可以使用sqlite3 my-db.sqlite 连接并运行诸如.tables 之类的命令来查看我之前创建的表,但我无法终生获得使用迁移创建的“nameOfTheNewTable”表。 (我也想在迁移中创建索引)。我尝试使用 "development",更改 config.json 中的值,例如主机、数据库(my-db、../my-db、my-db.sqlite)等。

这是一个很好的例子,在config.json 我把"database" : "bad-db" 和迁移的输出完全一样。完成后,找不到 bad-db.sqlite 文件。

【问题讨论】:

    标签: sqlite sequelize.js


    【解决方案1】:

    您需要在 config.json 中指定 'storage' 参数,以便 sequelize 知道将哪个文件用作 sqlite DB。

    Sequelize 默认为 sqlite 使用内存存储,因此它正在迁移内存中的数据库,然后退出,有效地破坏了它刚刚迁移的数据库。

    【讨论】:

    • 这很有意义。
    【解决方案2】:

    您很可能需要等待 migration.createTable 完成:

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

    【讨论】:

    • 好点。更新了我的代码以使用成功回调。输出已更改,但仍未创建表。
    • 看起来迁移无法知道 sqlite 使用哪个文件,因为 'storage' 参数不是从 config.json 读取的。我在 github 上打开了 issue #1050:github.com/sequelize/sequelize/issues/1050
    • 我满是便便。您只需要指定“存储”参数即可。
    猜你喜欢
    • 1970-01-01
    • 2020-11-09
    • 1970-01-01
    • 2018-01-29
    • 2023-03-14
    • 2012-09-08
    • 1970-01-01
    • 1970-01-01
    • 2018-07-10
    相关资源
    最近更新 更多