Comments (9)
Hi @arma-gast !
Can you share with us a sample of your code?
from lucid.
Somewhere in app/Model:
class Product extends Lucid {
colors() {
return this.belongsToMany('App/Model/Color');
}
}
Somewhere in app/Http/Controllers:
class SomeController {
* update(request, response) {
const productId = request.param('id'),
data = request.post();
// validation and error rendering
const product = yield Product.find(productId);
// attributes update
yield product.save();
yield product.colors().sync(data.productColors || []);
// redirect
}
}
I am updating product
's attributes with data from post (loaded in data
). Earlier, if no colors were selected, I received other error, something about requiring an array in .attach
. Now, if no colors selected, I get MISUSE error.
Is that enough?
from lucid.
Yes, that's perfect!
It seems like a bug. Let's wait for @thetutlage.
from lucid.
@arma-gast sync
should pass references
to the detach
method here. https://github.com/adonisjs/adonis-lucid/blob/develop/src/Lucid/Relations/BelongsToMany.js#L282
I am happy to accept the PR with tests
from lucid.
Created PR with failing test. Will try to resolve it later.
Update: resolved.
from lucid.
sync
works obvious: it removes all references than creates new ones. Thats why it does not pass references to detach
.
And syncing with an empty array should be allowed: it still an array, just zero length. Why should I check for it's length, call detach
if length is zero and sync
if it contains items?
from lucid.
Ohh yeah sync
is working as expected. (My bad)
With detach
it is more of a design question than the implementation question.
It will be over responsibility of detach
method to do the right thing inside a black box, whereas right now the expectations are clear. When you pass a parameter it should have values or do not pass anything to remove all the references.
With an empty check on the array, the layer will become too complicated. If there is an array remove selected, if array is empty do nothing and if there is no parameter passed remove everything. Which gives me less confidence on what really is going to happen with the detach method.
It should be the end user responsibility to handle the domain logic and try to be as explicit as possible within the code.
from lucid.
Hmmm... Looks like you are talking about empty-array check in attach
in my PR.
Here is my thoughts:
attach
expects array (or map) as first parameter.- Empty array is still an array.
- There is no way to prevent end user from passing an empty array to
attach
other than make a note in docs.
On the other hand, we can modify attach
to correctly process empty array: I say "attach no courses to student", and it does nothing. For me this is expected behaviour.
from lucid.
Fixed in 4.0
from lucid.
Related Issues (20)
- Factory with BelongsTo relationship fails with `this.factory is not a function` HOT 1
- Search path occasionally reverts to public for PG connection with custom schema defined HOT 3
- .useTransaction() is ignored and the default connection is used HOT 4
- useWhereRaw error "bind message supplies 2 parameters, but prepared statement "" requires 1" HOT 1
- V5: Repeatable read isolation doesn't work as expected
- `BaseModel.toObject()` fails with `null` nested BelongsTo relationship HOT 4
- FindManyBy return type error HOT 2
- Model delete while joining other tables generates wrong query HOT 4
- Missing exports
- If value is DateTime in newUpIfMissing, duplicate rows are created even if the value is the same
- Preload relationship does not work
- firstOrCreate() causes infinite loop in beforeUpdate() hook
- LucidModel: updateOrCreate not return row with default value HOT 1
- ModelPaginator export is not available in Lucid ORM [Adonis V6]
- `node ace db:truncate` throws the error deadlock detected HOT 2
- Report feedback error
- whereILike Not Working HOT 1
- Preload nullable foreign key isnt working when null HOT 1
- Clone method removes preloads HOT 3
- migration:fresh & db:wipe fail with "ERROR: table sqlite_master may not be modified" HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from lucid.