2016-05-10 5 views
6

私はdb:seed:allに1時間以上苦労しており、ゆっくりとこのことについて私の心を失っています。Sequelize-CLIシードャ - 定義されていないプロパティを読み取ることができません

'use strict'; 
module.exports = function (sequelize, DataTypes) { 
    var Car = sequelize.define('Cars', { 
    name: DataTypes.STRING, 
    type: DataTypes.INTEGER, 
    models: DataTypes.INTEGER 
    }, { 
     classMethods: { 
     associate: function (models) { 
      // associations can be defined here 
     } 
     } 
    }); 
    return Car; 
}; 

これは、移行にあり、正常に動作しますsequelize db:migrateを使用してデータベースに行く:

は、私は単純なモデルを持っています。

次は、シードファイル(2台の車)を挿入したかったのです。今、私はsequelize db:seed:allを実行すると、私はエラー、次の取得

'use strict'; 

module.exports = { 
    up: function (queryInterface, Sequelize) { 
    return queryInterface.bulkInsert(
     'Cars', 
     [ 
     { 
      name: "Auris", 
      type: 1, 
      models: 500, 
      createdAt: Date.now(), updatedAt: Date.now() 
     }, 
     { 
      name: "Yaris", 
      type: 1, 
      models: 500, 
      createdAt: Date.now(), updatedAt: Date.now() 
     } 
     ] 
    ); 
    }, 

    down: function (queryInterface, Sequelize) { 
    } 
}; 

: は、だから私は、コマンドにsequelize seed:create --name insertCars を走り、bulkInsertを追加

Loaded configuration file "config\config.json". 
Using environment "development". 
== 20160510132128-insertCars: migrating ======= 
Seed file failed with error: Cannot read property 'name' of undefined 

は、誰もがこれらのシーダーを実行している任意の経験を持っていますか?

{ 
    "development": { 
    "username": "mydbdude", 
    "password": "mydbdude", 
    "database": "Cars", 
    "host": "127.0.0.1", 
    "dialect": "mssql", 
    "development": { 
     "autoMigrateOldSchema": true 
    } 
    }, 
    ....other configs 
} 

EDIT::DBから出力:ここにあなたの情報について は私の設定ファイルである、私はまだ私は今、その後、この作業を取得することができていないので

Sequelize [Node: 5.9.1, CLI: 2.4.0, ORM: 3.23.0] 

Loaded configuration file "config\config.json". 
Using environment "development". 
No migrations were executed, database schema was already up to date. 
+0

こんにちは、 'sequelize db:migrate'の端末出力を表示できますか? – paolord

+0

@paolord私の元の質問を編集し、db:migrateの出力を下に追加しました – Tikkes

+0

あなたはどのDBの方言を使用していますか?私はPostgresと大文字と小文字を区別した名前で問題を抱えていました(つまり、 'cars'と' cars'テーブルpostgresが作成します) –

答えて

0

を移行し、私の解決策を投稿する私はどちらかといえばうまくいくが、カスタムは構築されている。

まず、シードしたいオブジェクトを含むJSONファイルを作成しました。

dataSources: [ 
    { 
     name: 'Pattern' 
    }, 
    { 
     name: 'Upload' 
    } 
] 

次、つまり、私は今、このすべての設定が行われ、次の(当然のトリミングバージョン)を含む自分のアプリケーションのサーバ側でseeder.js

var models = require('./../models'), 
sql = models.sequelize, 
Promise = models.Sequelize.Promise; 

var objects = require('./seed/objects'), //this is where my object file is 
dataSources = objects.dataSources; 


var express = require('express'); 

var seedDatabase = function() { 
    var promises = []; 
    promises.push(createDataSources()); 
    //more can be added to the promises 

    return Promise.all(promises); 
}; 

function createDataSources() { 
    return models.sequelize 
     .transaction(
      { 
       isolationLevel: models.sequelize.Transaction.ISOLATION_LEVELS.READ_COMMITTED 
      }, 
      function (t) { 
       return models.DataSource 
        .findAll({ 
         attributes: [ 
          'id' 
         ] 
        }) 
        .then(function (result) { 
         if (!result || result.length == 0) { 
          return models.DataSource 
           .bulkCreate(dataSources, { transaction: t }) 
           .then(function() { 
            console.log("DataSources created"); 
           }) 
         } 
         else { 
          console.log("DataSources seeder skipped, already objects in the database..."); 
          return; 
         } 
        }); 
      }) 
     .then(function (result) { 
      console.log("DataSources seeder finished..."); 
      return; 
     }) 
     .catch(function (error) { 
      console.log("DataSources seeder exited with error: " + error.message); 
      return; 
     }); 
}; 

module.exports = { 
    seedDatabase: seedDatabase 
} 

を実装している、私はこれを使用することができます私のアプリケーションがそうのように、私のシーダを実行するために開始しseedDatabase機能:

//includes and routes above, not interesting for this answer 
try { 
    umzug.up().then(function (migrations) { 
     for (var i = 0; i < migrations.length; i++) { 
      console.log("Migration executed: " + migrations[i].file); 
     } 

     console.log("Running seeder"); 
     seeder.seedDatabase().then(function() { //here I run my seeders 
      app.listen(app.get('port'), function() { 
       console.log('Express server listening on port ' + app.get('port')); 
      }); 
     }); 
    }); 
} 
catch (e) { 
    console.error(e); 
} 

今、それだけです、あなたをアプリケーションを実行すると、オブジェクトがすでにデータベースに挿入されている場合、シーダーはデータベースをチェックします。そうであれば、シーダーはスキップされ、そうでなければ挿入されます。

私はこれがあなたを助けてくれることを願っています。

関連する問題