diff --git a/packages/core/database/src/__tests__/collection.test.ts b/packages/core/database/src/__tests__/collection.test.ts index 8b919170b..49f928aef 100644 --- a/packages/core/database/src/__tests__/collection.test.ts +++ b/packages/core/database/src/__tests__/collection.test.ts @@ -3,6 +3,7 @@ import { Database } from '../database'; import { mockDatabase } from './index'; import { IdentifierError } from '../errors/identifier-error'; +const pgOnly = () => (process.env.DB_DIALECT == 'postgres' ? it : it.skip); describe('collection', () => { let db: Database; @@ -46,11 +47,7 @@ describe('collection', () => { expect(error.message.includes("Zero-column tables aren't supported in")).toBeTruthy(); }); - it('can create empty collection', async () => { - if (db.inDialect('sqlite', 'mysql')) { - return; - } - + pgOnly()('can create empty collection', async () => { db.collection({ name: 'empty', timestamps: false, diff --git a/packages/core/database/src/__tests__/inhertits/collection-inherits.test.ts b/packages/core/database/src/__tests__/inhertits/collection-inherits.test.ts index 6ceb71125..9f99fca6f 100644 --- a/packages/core/database/src/__tests__/inhertits/collection-inherits.test.ts +++ b/packages/core/database/src/__tests__/inhertits/collection-inherits.test.ts @@ -15,6 +15,49 @@ pgOnly()('collection inherits', () => { await db.close(); }); + it('should create inherits from empty table', async () => { + const empty = db.collection({ + name: 'empty', + timestamps: false, + autoGenId: false, + fields: [], + }); + + await db.sync({ + force: false, + alter: { + drop: false, + }, + }); + + const parent2 = db.collection({ + name: 'parent2', + }); + + const inherits = db.collection({ + name: 'inherits', + inherits: ['empty', 'parent2'], + fields: [{ type: 'string', name: 'name' }], + }); + + await db.sync({ + force: false, + alter: { + drop: false, + }, + }); + + expect(inherits instanceof InheritedCollection).toBeTruthy(); + + const record = await inherits.repository.create({ + values: { + name: 'test', + }, + }); + + expect(record.get('name')).toEqual('test'); + }); + it('should not throw error when fields have same type with parent', async () => { db.collection({ name: 'parent', diff --git a/packages/core/database/src/model.ts b/packages/core/database/src/model.ts index a9cd3232c..3dae4a135 100644 --- a/packages/core/database/src/model.ts +++ b/packages/core/database/src/model.ts @@ -159,7 +159,24 @@ export class Model maxSequenceVal) { @@ -89,12 +90,20 @@ export class SyncRunner { const sequenceTables = [...parentsDeep, tableName]; for (const sequenceTable of sequenceTables) { - await queryInterface.sequelize.query( - `alter table "${sequenceTable}" alter column id set default nextval('${maxSequenceName}')`, - { - transaction, - }, - ); + try { + await queryInterface.sequelize.query( + `alter table "${sequenceTable}" alter column id set default nextval('${maxSequenceName}')`, + { + transaction, + }, + ); + } catch (err) { + if (err.message.match(/column "id" of relation "\w+" does not exist/)) { + continue; + } + + throw err; + } } } diff --git a/packages/plugins/collection-manager/src/models/collection.ts b/packages/plugins/collection-manager/src/models/collection.ts index 739d27ec8..53870bc67 100644 --- a/packages/plugins/collection-manager/src/models/collection.ts +++ b/packages/plugins/collection-manager/src/models/collection.ts @@ -87,8 +87,9 @@ export class CollectionModel extends MagicAttributeModel { transaction: options?.transaction, }); + // postgres support zero column table, other database should not sync it to database // @ts-ignore - if (Object.keys(collection.model.tableAttributes).length == 0) { + if (Object.keys(collection.model.tableAttributes).length == 0 && !this.db.inDialect('postgres')) { return; }