From bc6924ffea80b3c480b99c2b321c1bde9efd7afe Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Tue, 3 Jan 2023 20:26:22 +0700 Subject: [PATCH] Crazy weird bug in ormish.js because I...never destroyed anything? Not sure why but I didn't actually ever run Model.destroy, and it turns out that it should be static like the other methods but I missed that. --- lib/ormish.js | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/lib/ormish.js b/lib/ormish.js index d05f5a9..b8975dc 100644 --- a/lib/ormish.js +++ b/lib/ormish.js @@ -244,22 +244,26 @@ export class Model { } /* - Delete this object from the database. It uses this object's `.id` to determine - which object to delete and uses this `knex` code: + Delete this object from the database. We use the word "destroy" because + `delete` is reserved. It uses this object's `.id` to determine which object + to delete and uses this `knex` code: ```javascript - await knex(this.table_name).where({id: this.id}).del(); + await knex(obj.table_name).where({id: obj.id}).del(); ``` As with all of ORMish it doesn't handle any relations or references when it does the delete so if you have constraints it will fail. Use `knex` directly in that case. + + + `obj Model` -- the object to destroy, just have id */ - async destroy() { + static async destroy(obj) { assert(this.table_name !== undefined, "You must set class variable table_name."); + assert(obj.id !== undefined, "No id in object to destroy."); await knex(this.table_name). - where({id: this.id}). + where({id: obj.id}). del(); }