zadam / trilium

Build your personal knowledge base with Trilium Notes
GNU Affero General Public License v3.0
27.2k stars 1.9k forks source link

(Bug report) Backend goes into bad state when relation updated by ETAPI #3998

Closed mm21 closed 1 year ago

mm21 commented 1 year ago

Trilium Version

v0.60.1-beta

What operating system are you using?

Ubuntu

What is your setup?

Server access only

Operating System Version

Ubuntu 22.04 LTS

Description

When a relation target is updated by ETAPI, the corresponding backlink to the original note target doesn't seem to be updated even after the relation is deleted. Once the relation is deleted, there's an error popup in the frontend whenever the original target is selected in the UI.

Steps to reproduce:

  1. Create note
  2. Create relation to root
  3. Change relation to created note
  4. Delete note

As a workaround, the relation can be changed back to root before deleting. However I haven't found a way to fix the error popup after it has already been deleted. Issue isn't seen if the relation isn't updated before being deleted.

Error logs

Console output from backend:

ERROR: get /api/note-map/:noteId/backlink-count threw exception: 'Note '4PiRRn7HNR5L' of attribute 'YSOOmz1Kk0YW', type 'relation', name 'myrelation' does not exist.', stack: Error: Note '4PiRRn7HNR5L' of attribute 'YSOOmz1Kk0YW', type 'relation', name 'myrelation' does not exist.
    at BAttribute.getNote (/mnt/hgfs/shared/trilium-latest2/trilium/src/becca/entities/battribute.js:133:19)
    at /mnt/hgfs/shared/trilium-latest2/trilium/src/routes/api/note_map.js:342:40
    at Array.filter (<anonymous>)
    at getFilteredBacklinks (/mnt/hgfs/shared/trilium-latest2/trilium/src/routes/api/note_map.js:342:10)
    at getBacklinkCount (/mnt/hgfs/shared/trilium-latest2/trilium/src/routes/api/note_map.js:355:20)
    at Function.cb (/mnt/hgfs/shared/trilium-latest2/trilium/src/routes/routes.js:411:34)
    at Function.sqliteTransaction (/mnt/hgfs/shared/trilium-latest2/trilium/node_modules/better-sqlite3/lib/methods/transaction.js:65:24)
    at Object.transactional (/mnt/hgfs/shared/trilium-latest2/trilium/src/services/sql.js:242:52)
    at /mnt/hgfs/shared/trilium-latest2/trilium/src/routes/routes.js:413:44
    at /mnt/hgfs/shared/trilium-latest2/trilium/node_modules/cls-hooked/context.js:118:13
304 GET /api/notes/root with 421 bytes took 2ms
JS Error: 500 GET note-map/root/backlink-count - Note '4PiRRn7HNR5L' of attribute 'YSOOmz1Kk0YW', type 'relation', name 'myrelation' does not exist.
Stack: Error
    at Object.logError (http://vm-dev-1:8080/assets/v0.60.1-beta/app/services/ws.js:24:20)
    at Object.throwError (http://vm-dev-1:8080/assets/v0.60.1-beta/app/services/toast.js:100:8)
    at reportError (http://vm-dev-1:8080/assets/v0.60.1-beta/app/services/server.js:124:22)
    at async Object.error (http://vm-dev-1:8080/assets/v0.60.1-beta/app/services/server.js:150:17)

The 500 GET error is seen in the frontend upon selecting the root note.

zadam commented 1 year ago

Hello, this is a problem in validation in ETAPI. Relation's target note cannot be changed. If you do want to change it, you need to delete the old relation and create a new one.

(And problems with updating the state in various places is exactly the motivation to make properties with references to entities immutable)