2016-04-05 152 views
1

我發現有兩種方法可以在遷移文件中編寫knex遷移。創建knex遷移

exports.up = function (knex) { 
    return knex.schema 
     .createTableIfNotExists('foo', function (table) { 
     table.increments('id').unique(); 
     table.string('foo1'); 
     table.string('foo2'); 
     }) 
    .createTableIfNotExists('bar', function (table) { 
     table.increments('bar1'); 
     table.string('bar2').index(); 
     }); 

或者

exports.up = function (knex) { 
    return Promise.all([ 
     knex.schema.createTableIfNotExists('foo', function (table) { 
     table.increments('id').unique(); 
     table.string('foo1'); 
     table.string('foo2'); 
     }), 
     knex.schema.createTableIfNotExists('bar', function (table) { 
     table.increments('bar1'); 
     table.string('bar2').index(); 
     }) 
    ]); 
} 

哪一個是做的正確方法嗎?

回答

1

Knex's github issue page

回答Ricardo Graca在這種情況下,它不會有所作爲。

你只使用基於承諾之一,如果你需要在 在做另一個表中的另一個改變之前的表中的一些變化。例如,如果 你需要從另一個表,並沒有一個 還不存在這些表引用某個表,您將創建第一個表(即 不依賴於任何一個)在承諾,然後當這個承諾 解決你會創建第二個表。這樣你就可以確保滿足相關性。

1

這是添加knex腳本以正確的方式,如承諾與查詢在knex處理的首選方式,因爲它們允許你從履行處理程序返回值。

exports.up = function (knex) { 
return Promise.all([ 
    knex.schema.createTableIfNotExists('foo', function (table) { 
    table.increments('id').unique(); 
    table.string('foo1'); 
    table.string('foo2'); 
    }), 
    knex.schema.createTableIfNotExists('bar', function (table) { 
    table.increments('bar1'); 
    table.string('bar2').index(); 
    }) 
]); 
}