adonisjs / lucid

AdonisJS SQL ORM. Supports PostgreSQL, MySQL, MSSQL, Redshift, SQLite and many more
https://lucid.adonisjs.com/
MIT License
1.03k stars 189 forks source link

Providing an empty array to .detach clears table. #901

Closed weltmx closed 1 year ago

weltmx commented 1 year ago

The docs specify that not providing detach with any parameter will remove all items.

// Remove projects with id 1, 2, 3
await user.related('projects').detach([1, 2, 3])

// Remove all projects
await user.related('projects').detach()

But, detach([]) will do the same. I just want to present the argument that providing an empty array, a state that can very often be naturally obtained through filtering, should not result in the dangerous outcome of clearing a table, but maybe just to exiting the query early.

thetutlage commented 1 year ago

The expectations can be subjective, especially when no input does clear all the rows. You can add a check before calling the detach method. For now, I am not changing any existing behaviour in the ORM.