2017-12-12 18 views
0

나는 거의 모든 것에 익숙하지 않습니다. 단일 마이그레이션 파일을 사용하여 MySQL 데이터베이스 (20 개 테이블)의 모든 테이블을 만들려고했습니다. 틀림없이Knex MySQL Migration "처리되지 않은 거부 오류 : 트랜잭션 쿼리가 이미 완료되었습니다"

Unhandled rejection Error: Transaction query already complete, run with DEBUG=knex:tx for more info

내가하지 않아도 :

exports.up = function(knex, Promise) { 

function createTable1() { 
    return knex.schema.createTableIfNotExists('Table1', (t) => { 
     t.increments('id').primary(); 
     t.string('col_1', 48).unique().notNullable(); 
     t.timestamps(true, true); 
    }).catch((e) => console.log(e)); 
} 

function createTable2() { 
    return knex.schema.createTableIfNotExists('Table2', (t) => { 
     t.increments('id').primary(); 
     t.string('col_1', 48).unique().notNullable(); 
     t.integer('someId').unsigned().references('Table1.id') 
     t.timestamps(true, true); 
    }).catch((e) => console.log(e)); 
} 

function createTable3() { 
    return knex.schema.createTableIfNotExists('Table3', (t) => { 
     t.increments('id').primary(); 
     t.string('col_1', 48).unique().notNullable(); 
     t.integer('someId').unsigned().references('Table1.id') 
     t.integer('someOtherId').unsigned().references('Table2.id') 
     t.timestamps(true, true); 
    }).catch((e) => console.log(e)); 
} 
... //similar functions for all 20 tables 

return Promise.all([ 
    createTable1() 
    .then(createTable2()) 
    .then(createTable3()) 
    ... 
    .then(createTable20()) 
    .catch((e) => console.log(e.sql)) 
    ]); 
} 

exports.down = function(knex, Promise) { 

    return knex.schema.dropTable('Table1') 
    .then(knex.schema.dropTable('Table2')) 
    .then(knex.schema.dropTable('Table3')) 
    ... 
    .then(knex.schema.dropTable('Table20')) 
    .catch((e) => console.log(e.sql)) 
}; 

나는 하나의 트랜잭션 마이그레이션 실행하지만, 다음과 같은 오류가 발생

에서 SQL 쿼리를 모두 실행 knex 예상 약속을 올바르게 사용하는 방법에 대한 확고한 이해와 나는 Promise.all 블록이 반드시 & execut 같은 순서로 SQL 쿼리를 처리 할 수 ​​있습니까? 각 테이블에 대해 별도의 마이그레이션 파일을 만드는 것이 더 합리적입니까?

+0

> 각 테이블에 대해 별도의 마이그레이션 파일을 만드는 것이 더 중요합니까? 나는 그것을하는 것이 더 일반적이라고 생각합니다. – langitbiru

답변

0

체인을 연결하는 대신 약속 체인에서 함수를 호출하고 있습니다. 첫 번째 함수는 실행되고 .then에있는 다른 함수와 연결되어야합니다. 또한 약속 체인을 뒤섞어 사용하고 Promise.all을 사용하는 것 같습니다.

는 각 테이블이 Promise.all 드롭 순차적으로 생성하려는 경우와 함수가 호출

return Promise.all([createTable1(), createTable2(), ..., createTable20()]) 
:

return createTable1() 
    .then(createTable2) 
    .then(createTable3) 
    ... 
    .then(createTable20) 
    .catch((e) => console.log(e.sql)) 

는이 같은 동시에 사용 Promise.all에서 N 테이블을 모두 만들려면