w3tecch / express-typescript-boilerplate

A delightful way to building a RESTful API with NodeJs & TypeScript by @w3tecch
MIT License
3.31k stars 905 forks source link

Migrations aren't working #98

Closed Fraccaman closed 6 years ago

Fraccaman commented 6 years ago

Running npm run setup, I get the following error:

Could not run seed { QueryFailedError: ER_NO_SUCH_TABLE: Table 'my_database.user' doesn't exist at new QueryFailedError (/Users/fraccaman/Projects/express-typescript-boilerplate/src/error/QueryFailedError.ts:7:9) at Query._callback (/Users/fraccaman/Projects/express-typescript-boilerplate/src/driver/mysql/MysqlQueryRunner.ts:157:37) at Query.Sequence.end (/Users/fraccaman/Projects/express-typescript-boilerplate/node_modules/mysql/lib/protocol/sequences/Sequence.js:88:24) at Query.ErrorPacket (/Users/fraccaman/Projects/express-typescript-boilerplate/node_modules/mysql/lib/protocol/sequences/Query.js:90:8) at Protocol._parsePacket (/Users/fraccaman/Projects/express-typescript-boilerplate/node_modules/mysql/lib/protocol/Protocol.js:279:23) at Parser.write (/Users/fraccaman/Projects/express-typescript-boilerplate/node_modules/mysql/lib/protocol/Parser.js:76:12) at Protocol.write (/Users/fraccaman/Projects/express-typescript-boilerplate/node_modules/mysql/lib/protocol/Protocol.js:39:16) at Socket.<anonymous> (/Users/fraccaman/Projects/express-typescript-boilerplate/node_modules/mysql/lib/Connection.js:103:28) at Socket.emit (events.js:159:13) at addChunk (_stream_readable.js:265:12) message: 'ER_NO_SUCH_TABLE: Table \'my_database.user\' doesn\'t exist', code: 'ER_NO_SUCH_TABLE', errno: 1146, sqlMessage: 'Table \'my_database.user\' doesn\'t exist', sqlState: '42S02', index: 0, sql: 'INSERT INTOuser(id,first_name,last_name,email) VALUES (\'25220231-36b6-4031-8ae1-6cf3739074bd\', \'Bruce\', \'Wayne\', \'bruce.wayne@wayne-enterprises.com\')', name: 'QueryFailedError', query: 'INSERT INTOuser(id,first_name,last_name,email) VALUES (?, ?, ?, ?)', parameters: [ '25220231-36b6-4031-8ae1-6cf3739074bd', 'Bruce', 'Wayne', 'bruce.wayne@wayne-enterprises.com' ] } The script called "db.seed" which runs "nps banner.seed && nps config && ts-node --transpileOnly ./src/lib/seed/cli.ts" failed with exit code 1 https://github.com/kentcdodds/nps/blob/v5.9.0/other/ERRORS_AND_WARNINGS.md#failed-with-exit-code The script called "setup.script" which runs "yarn install && nps db.drop && nps db.migrate && nps db.seed" failed with exit code 1 https://github.com/kentcdodds/nps/blob/v5.9.0/other/ERRORS_AND_WARNINGS.md#failed-with-exit-code

I think this is due to the fact that during the migration stage, the tables are not created.

nps is executingconfig: ts-node --transpileOnly ./commands/tsconfig.ts && ts-node --transpileOnly ./commands/ormconfig.ts query: SELECT * FROMINFORMATION_SCHEMA.COLUMNSWHERETABLE_SCHEMA= 'my_database' ANDTABLE_NAME= 'migrations' query: CREATE TABLEmy_database.migrations(idint NOT NULL AUTO_INCREMENT,timestampbigint NOT NULL,namevarchar(255) NOT NULL, PRIMARY KEY (id)) ENGINE=InnoDB query: SELECT * FROMmy_database.migrations`migrations No migrations are pending nps is executing db.seed : nps banner.seed && nps config && ts-node --transpileOnly ./src/lib/seed/cli.ts nps is executing banner.seed : ts-node --transpileOnly ./commands/banner.ts seed`

jahumes commented 6 years ago

@Fraccaman So, I had the same issue and spent an hour tracking it down. The problem stems from TypeORM looking for .env variables to see if your environment is where it should pull it's config. (See line 98 of ConnectionOptionsReader.ts) Since this is the name of the .env variable that this boilerplate uses to choose the database for typeorm, the ormconfig.json file is being ignored. All you have to is either uncomment the other typeorm configs in the .env file, or rename TYPEORM_CONNECTION to anything else in the .env file and the env.ts file.

jahumes commented 6 years ago

See https://github.com/w3tecch/express-typescript-boilerplate/pull/96

sandrooco commented 6 years ago

I can't generate a migration. @jahumes fix didn't work. Any other ideas?

jahumes commented 6 years ago

@sandrooco Please put a console.log(connectionOptions) after line 142 of node_modules/typeorm/connection/ConnectionOptionsReader.js and see if the output is the expected config. If the console.log is hit at all, then it is using the .env instead of the ormconfig.json. If you installed typeorm globally, you will need to make this change to the global version of it if you are using typeorm migration:generate.

Also, please provide the output of the generate command.

disovi commented 6 years ago

@jahumes is right, the problem is in TYPEORM_CONNECTION from .env file which clashes with TypeORM config loader. This bug was introduced in https://github.com/w3tecch/express-typescript-boilerplate/pull/95 PR. Two solutions:

  1. Use master branch

or

  1. Rename TYPEORM_CONNECTION to TYPEORM_CONNECTION_BP in .env and in env.ts
sandrooco commented 6 years ago

Thank you, that worked.