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
Relationships do not update on save #468
Comments
I don't know if this is a bug. I try to think about it, because i also get your point that normally it is an The thing is if you retrieve a model and don't eager load a relation, than you get it as For now surly it should work if you use function unlink() {
useRepo(Comment).save({
id: 1,
postId: null,
// post: null
});
// The expected behavior here is if `post` is null on save, pinia-orm should not retain the `postId`.
// The current undesired behavior is `postId` is still 1, causing pinia-orm to show the relationship still exists.
console.log(JSON.stringify(useRepo(Comment).find(1), null, 2));
} |
To clarify, I'm not expecting it to If you retrieve a model and don't eager load a relation, I think the more precise behavior could be that the relational fields don't exist period - instead of being null. Then, when saving it back, it keeps any relationships because the |
I know what you mean. But you have to think more the Yes there is an other workaround for you: export class Comment extends Model {
static fields() {
return {
id: this.uid(),
userId: this.attr(null),
user: this.belongsTo(User, "userId"),
postId: this.attr(null),
post: this.belongsTo(Post, "postId"),
};
}
static updating (model) {
// change values before saving
if (model.post === null) {
model.postId = null
}
}
}
Comment.entity = "comments"; |
I'm satisfied with that workaround and this issue may be closed. One last question: to make the workaround even better - From within the updating() method:
Thanks again for all of the help! |
For a cup of coffee ☕ i will answer your question 😜 ... import { Relation, Model } from 'pinia-orm'
export class Comment extends Model {
static fields() {
return {
id: this.uid(),
userId: this.attr(null),
user: this.belongsTo(User, "userId"),
postId: this.attr(null),
post: this.belongsTo(Post, "postId"),
};
}
static updating (model) {
// change values before saving
if (model.post === null && model.$fields().post instanceof Relation // or "instanceof BelongsTo") {
const relationForeignKey = model.$fields().post.foreignKey
model[relationForeignKey] = null
}
}
}
Comment.entity = "comments"; |
Enjoy your coffee 😃 |
@salcedo Even per month ! Thanks a lot ❤️ . It means a lot to me! I new name on the readme list inoming 😃. |
@salcedo If you haven't done it already you can make a import { BelongsTo, Model } from 'pinia-orm'
export class BaseModel extends Model {
static updating (model) {
const fields = model.$fields()
for (const name in fields) {
if (fields[name] instanceof BelongsTo && model[name] === null) {
model[fields[name].foreignKey] = null
}
}
}
}
export class Comment extends BaseModel { ... } |
Now that I've had a chance to try this, it doesn't work. Because model[name] is derived, it will always be an undefined property of model. Is there a way to get access to the incoming data in these hooks? |
Really? in my unit tests it worked, but maybe you have a differnet approach of data saving. what about trying Update: Ok i have your point now. I need to enhance the hooks so it works what you need. |
Should be working now with the new release v1.1.0 😉 import { BelongsTo, Model } from 'pinia-orm'
export class BaseModel extends Model {
static updating (model, record) {
const fields = model.$fields()
for (const name in fields) {
if (fields[name] instanceof BelongsTo && record[name] === null) {
model[fields[name].foreignKey] = null
}
}
}
}
export class Comment extends BaseModel { ... } |
Forgot to mention this earlier when you said that it worked fine during unit tests: It seems I worked around this by getting at *.constructor.name which is probably not ideal. ETA: e.g. instead of importing BelongsTo (seems it's not exported except during unit tests), Aaaaand. Also now every property of relations are declared public so they can be accessed. So you can make queries dependend on field declarations. looks like you caught it too! :) |
You are absoluty right. The classes weren't exported. So this couldn't work ^^. But at least now it does 💯 |
Thanks again! I will update the BaseModel idea you provided using these new features and report back if there is any further issues. |
If relationship fields sent from a backend are null - or in the case of a many to many relationship - the contents of an array have changed, pinia-orm does not appear to keep track of these changes by updating the id fields and/or pivot tables.
Please see https://github.com/salcedo/pinia-orm-relationship-unlinking for reproduction of this issue.
The text was updated successfully, but these errors were encountered: