diff --git a/server/models/Collection.js b/server/models/Collection.js index aae4f7a7f..bfd67ed43 100644 --- a/server/models/Collection.js +++ b/server/models/Collection.js @@ -304,7 +304,12 @@ Collection.prototype.updateDocument = async function ( }; this.documentStructure = updateChildren(this.documentStructure); - await this.save({ transaction }); + + // Sequelize doesn't seem to set the value with splice on JSONB field + // https://github.com/sequelize/sequelize/blob/e1446837196c07b8ff0c23359b958d68af40fd6d/src/model.js#L3937 + this.changed("documentStructure", true); + + await this.save({ fields: ["documentStructure"], transaction }); await transaction.commit(); } catch (err) { if (transaction) { @@ -357,10 +362,11 @@ Collection.prototype.removeDocumentInStructure = async function ( document.id ); - await this.save({ - ...options, - transaction, - }); + // Sequelize doesn't seem to set the value with splice on JSONB field + // https://github.com/sequelize/sequelize/blob/e1446837196c07b8ff0c23359b958d68af40fd6d/src/model.js#L3937 + this.changed("documentStructure", true); + + await this.save({ ...options, fields: ["documentStructure"], transaction }); await transaction.commit(); } catch (err) { if (transaction) { diff --git a/server/models/Collection.test.js b/server/models/Collection.test.js index cad483199..17e866359 100644 --- a/server/models/Collection.test.js +++ b/server/models/Collection.test.js @@ -144,7 +144,9 @@ describe("#updateDocument", () => { await collection.updateDocument(newDocument); - expect(collection.documentStructure[0].children[0].title).toBe( + const reloaded = await Collection.findByPk(collection.id); + + expect(reloaded.documentStructure[0].children[0].title).toBe( "Updated title" ); }); @@ -224,8 +226,10 @@ describe("#removeDocument", () => { // Remove the document await collection.deleteDocument(newDocument); - expect(collection.documentStructure.length).toBe(1); - expect(collection.documentStructure[0].children.length).toBe(0); + const reloaded = await Collection.findByPk(collection.id); + + expect(reloaded.documentStructure.length).toBe(1); + expect(reloaded.documentStructure[0].children.length).toBe(0); const collectionDocuments = await Document.findAndCountAll({ where: {