[Potential bug] undefined database on table.renameColumn
Author: BrainCrumbzCreated Oct 30, 2024Updated Sep 23, 2026
LabelsNG_bugNG_difficulty_medium
Environment
Knex version: 3.1.0 Database + version: (irrelevant) MySQL 8.0.32 OS: Windows DB driver library: mysql2 3.11.3
Bug
- Explain what kind of behaviour you are getting and how you think it should do
Running a migration, during which a new column is created. The new column is populated from an existing one, but this is not needed in order to show the issue, it can be skipped. Then the old column is dropped. Then the new one is renamed as the old one. When doing the rename, there's a runtime error.
The error happens even if I rename the new column to some other name.
The error does not happen if I comment out the rename instruction.
- Error message
migration failed with error: Cannot read properties of undefined (reading 'database')
Cannot read properties of undefined (reading 'database')
TypeError: Cannot read properties of undefined (reading 'database')
at Client_MySQL2.database (D:\myproject\node_modules\knex\lib\client.js:375:36)
at TableCompiler_MySQL.getFKRefs (D:\myproject\node_modules\knex\lib\dialects\mysql\schema\mysql-tablecompiler.js:200:21)
at Runner.output (D:\myproject\node_modules\knex\lib\dialects\mysql\schema\mysql-tablecompiler.js:86:25)
at Client_MySQL2.processResponse (D:\myproject\node_modules\knex\lib\dialects\mysql\index.js:156:39)
at D:\myproject\node_modules\knex\lib\execution\runner.js:151:35
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async Runner.ensureConnection (D:\myproject\node_modules\knex\lib\execution\runner.js:318:14)
at async Runner.run (D:\myproject\node_modules\knex\lib\execution\runner.js:30:19)
at Object.up (d:\myproject\db\migrations\20241030161155_alter_iso_string_to_datetime.ts:26:3)
at async Migrator._runBatch (D:\myproject\node_modules\knex\lib\migrations\migrate\Migrator.js:382:19)
- Reduced test code, for example in https://npm.runkit.com/knex
We just found out that runkit does not work. Here's the test code:
export async function up(knex: Knex): Promise<void> {
// (re)create table
await knex.schema.dropTableIfExists('temp_table');
await knex.schema.createTable('temp_table', table => {
table.increments('temp_table_id').primary();
table.string('timestamp', 45).notNullable();
});
// add new column
await knex.schema.alterTable('temp_table', table => {
table
.datetime('timestamp_new', { precision: 3})
.notNullable();
});
// drop old column
await knex.schema.alterTable('temp_table', table => {
table.dropColumn('timestamp');
});
// rename new column as old one
await knex.schema.alterTable('temp_table', table => {
// !!!!!!!!!!!!!!!!!!!!!!! this causes the error
table.renameColumn('timestamp_new', 'timestamp');
});
// destroy table
await knex.schema.dropTable('temp_table');
}
export async function down(knex: Knex): Promise<void> {
//
}Source: knex/knex