chore: should update belongs to many through table when it is a view (#2635)

This commit is contained in:
ChengLei Shao 2023-09-13 07:15:43 +08:00 committed by GitHub
parent 3670d670ac
commit 6f68506eb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 0 additions and 184 deletions

View File

@ -3,185 +3,6 @@ import { Database } from '../database';
import { updateAssociations } from '../update-associations'; import { updateAssociations } from '../update-associations';
import { mockDatabase } from './'; 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('update associations', () => {
describe('belongsTo', () => { describe('belongsTo', () => {
let db: Database; let db: Database;

View File

@ -373,11 +373,6 @@ export async function updateMultipleAssociation(
return false; 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)) { if (!['undefined', 'string', 'number', 'object'].includes(typeof value)) {
return false; return false;
} }