From 6f68506eb4a66d77d9e74120544b495841b213a2 Mon Sep 17 00:00:00 2001 From: ChengLei Shao Date: Wed, 13 Sep 2023 07:15:43 +0800 Subject: [PATCH] chore: should update belongs to many through table when it is a view (#2635) --- .../src/__tests__/update-associations.test.ts | 179 ------------------ .../core/database/src/update-associations.ts | 5 - 2 files changed, 184 deletions(-) diff --git a/packages/core/database/src/__tests__/update-associations.test.ts b/packages/core/database/src/__tests__/update-associations.test.ts index 1f3352bf7..a212d5d47 100644 --- a/packages/core/database/src/__tests__/update-associations.test.ts +++ b/packages/core/database/src/__tests__/update-associations.test.ts @@ -3,185 +3,6 @@ import { Database } from '../database'; import { updateAssociations } from '../update-associations'; import { mockDatabase } from './'; -describe('update belongs to many with view as through table', () => { - let db: Database; - beforeEach(async () => { - db = mockDatabase({ - tablePrefix: '', - }); - await db.clean({ - drop: true, - }); - }); - - afterEach(async () => { - await db.close(); - }); - - it('should not update through table', async () => { - const Order = db.collection({ - name: 'orders', - fields: [ - { - type: 'string', - name: 'name', - }, - { - type: 'hasMany', - name: 'orderItems', - foreignKey: 'order_id', - target: 'orderItems', - }, - ], - }); - - const OrderItem = db.collection({ - name: 'orderItems', - timestamps: false, - fields: [ - { - type: 'integer', - name: 'count', - }, - { - type: 'belongsTo', - name: 'item', - target: 'items', - foreignKey: 'item_id', - }, - { - type: 'belongsTo', - name: 'order', - target: 'orders', - foreignKey: 'order_id', - }, - ], - }); - - const Item = db.collection({ - name: 'items', - fields: [{ name: 'name', type: 'string' }], - }); - - await db.sync(); - - const viewName = 'order_item_view'; - - const dropViewSQL = `DROP VIEW IF EXISTS ${viewName}`; - await db.sequelize.query(dropViewSQL); - - const viewSQL = `CREATE VIEW ${viewName} as SELECT orders.*, items.name as item_name FROM ${OrderItem.quotedTableName()} as orders INNER JOIN ${Item.quotedTableName()} as items ON orders.item_id = items.id`; - - await db.sequelize.query(viewSQL); - - const OrderItemView = db.collection({ - name: viewName, - view: true, - schema: db.inDialect('postgres') ? 'public' : undefined, - fields: [ - { - type: 'bigInt', - name: 'order_id', - }, - { - type: 'bigInt', - name: 'item_id', - }, - ], - }); - - await db.sync(); - - Order.setField('items', { - type: 'belongsToMany', - target: 'orderItems', - through: viewName, - foreignKey: 'order_id', - otherKey: 'item_id', - sourceKey: 'id', - targetKey: 'id', - }); - - await db.sync(); - - const order1 = await db.getRepository('orders').create({ - values: { - name: 'order1', - orderItems: [ - { - count: 1, - item: { - name: 'item1', - }, - }, - { - count: 2, - item: { - name: 'item2', - }, - }, - ], - }, - }); - - const item1 = await db.getRepository('items').findOne({ - filter: { - name: 'item1', - }, - }); - - const item2 = await db.getRepository('items').findOne({ - filter: { - name: 'item2', - }, - }); - - await db.getRepository('orders').update({ - filterByTk: order1.get('id'), - values: { - name: 'order1', - orderItems: [ - { - count: 1, - item: { - id: item1.get('id'), - }, - }, - { - count: 2, - item: { - id: item2.get('id'), - }, - }, - { - count: 3, - item: { - name: 'item3', - }, - }, - ], - items: [ - { - id: item1.get('id'), - }, - { - id: item2.get('id'), - }, - ], - }, - }); - - const order1AfterUpdate = await db.getRepository('orders').findOne({ - filter: { - name: 'order1', - }, - appends: ['items'], - }); - - expect(order1AfterUpdate.get('items').length).toBe(3); - }); -}); - describe('update associations', () => { describe('belongsTo', () => { let db: Database; diff --git a/packages/core/database/src/update-associations.ts b/packages/core/database/src/update-associations.ts index bfee254d9..4308e3f14 100644 --- a/packages/core/database/src/update-associations.ts +++ b/packages/core/database/src/update-associations.ts @@ -373,11 +373,6 @@ export async function updateMultipleAssociation( return false; } - // @ts-ignore skip update association if through model is a view - if (association.through && association.through.model.options.view) { - return false; - } - if (!['undefined', 'string', 'number', 'object'].includes(typeof value)) { return false; }