sourcecode

이미 정규화된 vuex-orm 데이터베이스에 데이터를 삽입하는 중

copyscript 2022. 8. 21. 19:51
반응형

이미 정규화된 vuex-orm 데이터베이스에 데이터를 삽입하는 중

내가 받은 데이터가 이미 정규화되었다고 가정하거나 최소한 관계가 있다고 가정해봐이 데이터를 vuex-orm 데이터베이스에 삽입하려면 어떻게 해야 합니까?

JSON 데이터의 예:

{
  "carmodel": [
    {
      "id": 1,
      "title": "M3",
      "manufacturer_id": 1
    },
    {
      "id": 2,
      "title": "a-class"
      "manufacturer_id": 2
    }
  ],
  "manufacturer": [
    {
      "id": 1,
      "title": "BMW"
    },
    {
      "id": 2,
      "title": "Mercedes"
    }
  ]
}

제조업체 모델과 자동차 모델은 다음과 같이 삽입됩니다.

Manufacturer.insert({ data: response.data.manufacturer })
CarModel.insert({ data: response.data.carmodel })

이 모델은 동작하지 않습니다.

import { Model } from '@vuex-orm/core'
import Manufacturer from '@/models/Manufacturer'

export default class CarModel extends Model {
  static entity = 'carModels'

  static fields () {
    return {
      id: this.attr(null),
      title: this.string(''),
      manufacturer: this.hasOne(Manufacturer, 'manufacturer_id')
    }
  }
}

그래, 알 것 같아대신, 같은 모델의 manufacturer_id를 사용해야 합니다.

import { Model } from '@vuex-orm/core'
import Manufacturer from '@/models/Manufacturer'

export default class CarModel extends Model {
  static entity = 'carModels'

  static fields () {
    return {
      id: this.attr(null),
      title: this.string(''),
      manufacturer_id: this.attr(null),
      manufacturer: this.belongsTo(Manufacturer, 'manufacturer_id')
    }
  }
}

언급URL : https://stackoverflow.com/questions/61618081/inserting-data-in-vuex-orm-database-that-is-already-normalized

반응형