Skip to content

persistBy:create , data relation is missing #120

@mean-cj

Description

@mean-cj

Describe the bug

Define persistBy: create , belongsTo has overwrite new data
then relation data is missing.

   return this.post('url', {}, {
            persistBy: 'create'
          }
   )

Steps to reproduce the bug

1. Define models

export default class Seatextends Model {
  static entity = 'seat'
  static fields = () => {
    return {
      _id: this.attr(null),
      name: this.attr(null),
    }
  }
}
export default class Order extends Model {
  static entity = 'order'

  static fields = () => {
    return {
      _id: this.attr(null),
      seat_id: this.attr(null),
      seat: this.belongsTo(Seat, 'seat_id'),
    }
  }

  static apiConfig = {
    actions: {
      fetch() {
        return this.post('/fetch',{},{ persistBy: 'create' });
      },
   }
}

2. Create data

Seat.insert({ _id:1, name:"Table A"  });
Seat.insert({ _id:2, name:"Table B"  });
Seat.insert({ _id:3, name:"Table C"  });
Seat.insert({ _id:4, name:"Table D"  });
Seat.insert({ _id:5, name:"Table E"  });

3. Test CALL API :: Order.api().fetch();

Response data

[
  {  _id: 1, seat_id: 1, seat:{_id:1, name:'Table A'} }
  {  _id: 2, seat_id: 4, seat:{_id:4, name:'Table D'} }
  {  _id: 3, seat_id: 5, seat:{_id:5, name:'Table E'} }  
]

The same problem

Order.create({
 data: [
  {  _id: 1, seat_id: 1, seat:{_id:1, name:'Table A'} }
  {  _id: 2, seat_id: 4, seat:{_id:4, name:'Table D'} }
  {  _id: 3, seat_id: 5, seat:{_id:5, name:'Table E'} }  
]
});

4. See error
Seat id 2, 3 is missing

Seat.query().get()
[
  { _id:1, name:"Table A"  },
  { _id:4, name:"Table D"  },
  { _id:5, name:"Table E"  }
]

Expected behavior

What do you think,

can set persistBy option on relation fields.

  static fields = () => {
    return {
      _id: this.attr(null),
      seat_id: this.attr(null),
      seat: this.persistBy('insertOrUpdate').belongsTo(Seat, 'seat_id'),
    }
  }

can define on API Action method

  static apiConfig = {
    actions: {
      fetch() {
        return this.post('/fetch',{},{ 
            persistBy: 'create' ,
            persistRelations: [{ seat:'insertOrUpdate'}]
        });
      },
   }

can define persistRelations on vuex-orm core

Order.create({
 data: [
     {  _id: 1, seat_id:1, seat:{_id:1} }
     {  _id: 2, seat_id:4, seat:{_id:4} }
     {  _id: 3, seat_id:5, seat:{_id:5} }  
  ],
  {
     persistRelations: [{ seat:'insertOrUpdate' }]
  }
});

Versions

  • Vuex ORM Axios: dev
  • Vuex ORM: 0.36.3

Thank you.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions