chore: should update belongs to many through table when it is a view (#2635)
This commit is contained in:
parent
3670d670ac
commit
6f68506eb4
@ -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;
|
||||
|
@ -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;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user