New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
One To Many (Polymorphic) relationship is not working with custom primaryKey #714
Comments
Can someone fix this, please :) |
I can confirm this is a bug. The relation is attempting to acquire the primary key from the related entity (in this case I don't know if there will be more releases permissible on this branch (though, the repo does state no new "features", not bug fixes). But while that is established, you can patch this issue with a temporary fix. Create a plugin file and throw this in: // MorphManyPatch.js
export default {
install (components) {
components.MorphMany.prototype.attach = function (key, record, data) {
const relatedItems = data[this.related.entity]
key.forEach((id) => {
const relatedItem = relatedItems[id]
relatedItem[this.id] = relatedItem[this.id] || this.model.getIdFromRecord(record)
relatedItem[this.type] = relatedItem[this.type] || this.model.entity
})
}
}
} And install it before you install Vuex ORM to Vuex. See updated sandbox https://codesandbox.io/s/eloquent-snowflake-6lrz7 |
Should this be added to the docs maybe? At least for now? I was stuck on this for quite a while, and in the end the only thing I needed to do was remove the custom primary key on the model. It is not something you expect and not something you will figure out without going to the issues on github. |
Thanks, @cuebit, for taking the time to generate this solution. |
Describe the bug
Just imagine 2 models, Product and Attribute.
Product have One to Many (Polymorphic) relationship. Product model has own custom primaryKey (for example - uid).
The issue is that those attributes are inserted with wrong reference_id which is product.id, should be product.uid.
Steps to reproduce the bug
Product model
Attribute model
Insert data
Get data
Expected behaviour
const products = Product.query().with("attributes").get();
console.log('products', products);
This part of code should return product object with all attributes and each attribute should contain product.uid value as reference_id, but in this case its always product.id (1001).
Versions
Link to minimal reproduction
Codesandbox: https://codesandbox.io/s/happy-dream-qbi0e?fontsize=14&hidenavigation=1&theme=dark
The text was updated successfully, but these errors were encountered: