radmen / adonis-lucid-soft-deletes

Experimental implementation of soft-deletes for Adonis Lucid

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Soft deletes stop working after clone()

padinko opened this issue · comments

Hi,

When there is no clone, everything is going good

const query = Model.query();
query.on('query', console.log);
await query.fetch();
// select * from "models" where "models"."deleted_at" is null

but when i clone the model; cloned model have soft delete in queries, but original model lost it.

const query = Model.query();
query.on('query', console.log);
await query.clone().on('query', console.log).fetch();
// select * from "models" where "models"."deleted_at" is null
await query.fetch();
// select * from "models"

@padinko thank you for the issue. Presumbly this is a bug in the Lucid.

I've followed similar steps as you mentioned, yet got a bit different results.

const query = Model.query()
const clone = query.clone()

query.toSQL() // select * from `models` where `models`.`deleted_at` is null
clone.toSQL() // select * from `models`

The problem is not with this particular trait, but global scopes which are not transferred to the cloned query.

class Test extends Model {
    static boot() {
        super.boot()
        this.addGlobalScope(query => {
            query.where('foo', 'bar')
        })
    }
}

const query = Test.query()
const clone = query.clone()

query.toSQL() // select * from `tests` where `tests`.`foo` = ?
clone.toSQL() // select * from `models`

I'd suggest reporting this issue in the Lucid issues tracker.

Reported here adonisjs/lucid#483

Thank you!