Avoid foreign index constraint error when saving message attachments
This commit is contained in:
parent
34336ff284
commit
ab29fc7953
1 changed files with 15 additions and 6 deletions
|
@ -2830,16 +2830,25 @@ function saveMessage(
|
||||||
} satisfies Omit<MessageTypeUnhydrated, 'json'>;
|
} satisfies Omit<MessageTypeUnhydrated, 'json'>;
|
||||||
|
|
||||||
if (id && !forceSave) {
|
if (id && !forceSave) {
|
||||||
db.prepare(
|
const result = db
|
||||||
// UPDATE queries that set the value of a primary key column can be very slow when
|
.prepare(
|
||||||
// that key is referenced via a foreign key constraint, so we are careful to exclude
|
// UPDATE queries that set the value of a primary key column can be very slow when
|
||||||
// it here.
|
// that key is referenced via a foreign key constraint, so we are careful to
|
||||||
`
|
// exclude it here.
|
||||||
|
`
|
||||||
UPDATE messages SET
|
UPDATE messages SET
|
||||||
${MESSAGE_NON_PRIMARY_KEY_COLUMNS.map(name => `${name} = $${name}`).join(', ')}
|
${MESSAGE_NON_PRIMARY_KEY_COLUMNS.map(name => `${name} = $${name}`).join(', ')}
|
||||||
WHERE id = $id;
|
WHERE id = $id;
|
||||||
`
|
`
|
||||||
).run({ ...payloadWithoutJson, json: objectToJSON(dataToSaveAsJSON) });
|
)
|
||||||
|
.run({ ...payloadWithoutJson, json: objectToJSON(dataToSaveAsJSON) });
|
||||||
|
|
||||||
|
if (result.changes === 0) {
|
||||||
|
// Message has been deleted from DB
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
strictAssert(result.changes === 1, 'One row should have been changed');
|
||||||
|
|
||||||
if (normalizeAttachmentData) {
|
if (normalizeAttachmentData) {
|
||||||
saveMessageAttachments(db, message);
|
saveMessageAttachments(db, message);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue