diff --git a/lib/dialects/postgres/schema/pg-columncompiler.js b/lib/dialects/postgres/schema/pg-columncompiler.js index cc49f883cd..5fb7fa2580 100644 --- a/lib/dialects/postgres/schema/pg-columncompiler.js +++ b/lib/dialects/postgres/schema/pg-columncompiler.js @@ -72,22 +72,16 @@ class ColumnCompiler_PG extends ColumnCompiler { useTz = !withoutTz; } useTz = typeof useTz === 'boolean' ? useTz : true; - precision = precision ? '(' + precision + ')' : ''; + precision = + precision !== undefined && precision !== null + ? '(' + precision + ')' + : ''; return `${useTz ? 'timestamptz' : 'timestamp'}${precision}`; } timestamp(withoutTz = false, precision) { - let useTz; - if (isObject(withoutTz)) { - ({ useTz, precision } = withoutTz); - } else { - useTz = !withoutTz; - } - useTz = typeof useTz === 'boolean' ? useTz : true; - precision = precision ? '(' + precision + ')' : ''; - - return `${useTz ? 'timestamptz' : 'timestamp'}${precision}`; + return this.datetime(withoutTz, precision); } // Modifiers: diff --git a/test/unit/schema-builder/postgres.js b/test/unit/schema-builder/postgres.js index 48ca199630..2caee2ac6f 100644 --- a/test/unit/schema-builder/postgres.js +++ b/test/unit/schema-builder/postgres.js @@ -1528,6 +1528,19 @@ describe('PostgreSQL SchemaBuilder', function () { ); }); + it('adding timestamp with options object but precision 0 - #4784', () => { + tableSql = client + .schemaBuilder() + .table('users', (table) => { + table.timestamp('foo', { useTz: false, precision: 0 }); + }) + .toSQL(); + equal(1, tableSql.length); + expect(tableSql[0].sql).to.equal( + 'alter table "users" add column "foo" timestamp(0)' + ); + }); + it('adding datetime with options object', () => { tableSql = client .schemaBuilder() @@ -1567,6 +1580,19 @@ describe('PostgreSQL SchemaBuilder', function () { ); }); + it('adding datetime with options object but precision 0 - #4784', () => { + tableSql = client + .schemaBuilder() + .table('users', (table) => { + table.datetime('foo', { useTz: false, precision: 0 }); + }) + .toSQL(); + equal(1, tableSql.length); + expect(tableSql[0].sql).to.equal( + 'alter table "users" add column "foo" timestamp(0)' + ); + }); + it('adding timestamps', () => { tableSql = client .schemaBuilder()