Objection.js: possible relation upsertGraph bug as of 1.5.0

Created on 22 Jan 2019  路  5Comments  路  Source: Vincit/objection.js

1.4.0 would include cart_id key automatically
1.5.0 doesnt

const userCart = await Cart.query()
    .where('user_id', 10)
    .eager('items')
    .first();

await userCart
    .$relatedQuery('items')
    .upsertGraph([{
        // cart_id: 1 <--- set automatically by objection in 1.4.0
        product_id: 100,
        quantity: 2,
    }]);
export default class Cart extends BaseModel {
    static tableName = 'cart';

    id: number;
    user_id: number;
    items: CartItem[];

    static relationMappings = (): RelationMappings => ({
        items: {
            relation: Model.HasManyRelation,
            modelClass: CartItem,
            join: {
                from: 'cart.id',
                to: 'cart_item.cart_id',
            },
        },
    })
}

export default class CartItem extends BaseModel {
    static tableName = 'cart_item';

    id: number;
    cart_id: number;
    product_id: number;
    quantity: number;
}

Most helpful comment

All 5 comments

upsertGraph is not supposed to work like that. If it did work previously, it was a bug/accidental feature. There's this issue that will cause this scenario to throw in the future.

Ok thanks for quick reply

@koskimas it would be nice to highlight this in the docs for upsertGraph. I was always under the impression it's similar to how inserts work as demonstrated in the docs:
image

that might explain a bug or two actually :pensive:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

falkenhawk picture falkenhawk  路  4Comments

purepear picture purepear  路  3Comments

AhmadRaza786 picture AhmadRaza786  路  3Comments

nicolaracco picture nicolaracco  路  3Comments

haywirez picture haywirez  路  3Comments