Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/pinia-orm/src/model/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -817,7 +817,7 @@ export class Model {

if (this.$hasCompositeKey()) {
const compositeKey = this.$getCompositeKey(record)
return concatCompositeKey ? compositeKey?.join('') ?? null : compositeKey
return concatCompositeKey ? '[' + compositeKey?.join(',') + ']' : compositeKey
}

const id = record[this.$getKeyName() as string]
Expand Down
10 changes: 3 additions & 7 deletions packages/pinia-orm/src/query/Query.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { Pinia } from 'pinia'
import { acceptHMRUpdate } from 'pinia'
import {
assert, compareWithOperator, generateKey,
compareWithOperator,
generateKey,
groupBy,
isArray,
isEmpty,
Expand Down Expand Up @@ -859,11 +860,6 @@ export class Query<M extends Model = Model> {
destroy(ids: (string | number)[]): Collection<M>
destroy(id: string | number): Item<M>
destroy (ids: any): any {
assert(!this.model.$hasCompositeKey(), [
'You can\'t use the `destroy` method on a model with a composite key.',
'Please use `delete` method instead.'
])

return isArray(ids) ? this.destroyMany(ids) : this.destroyOne(ids)
}

Expand Down Expand Up @@ -928,7 +924,7 @@ export class Query<M extends Model = Model> {
if (fields[name] instanceof Relation && relation.onDeleteMode && model[name]) {
const models = isArray(model[name]) ? model[name] : [model[name]]
const relationIds = models.map((relation: M) => {
return relation[relation.$getLocalKey()]
return relation.$getKey(undefined, true)
})
const record: Record<string, any> = {}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ describe('feature/repository/delete_with_relations', () => {
class Comment extends Model {
static entity = 'comments'

static primaryKey = ['id', 'comment_id']

@Num(0) declare id: number
@Num(0) declare comment_id: number
@Num(0) declare postId: number
@Str('') declare title: string
}
Expand Down Expand Up @@ -111,8 +114,8 @@ describe('feature/repository/delete_with_relations', () => {
id: 1,
title: 'Title 01',
comments: [
{ id: 3, title: 'Title 03' },
{ id: 4, title: 'Title 04' }
{ id: 3, comment_id: 3, title: 'Title 03' },
{ id: 4, comment_id: 3, title: 'Title 04' }
],
extraComments: [
{ id: 3, title: 'Title 03' },
Expand All @@ -131,8 +134,8 @@ describe('feature/repository/delete_with_relations', () => {
id: 3,
title: 'Title 03',
comments: [
{ id: 1, title: 'Title 01' },
{ id: 2, title: 'Title 02' }
{ id: 1, comment_id: 3, title: 'Title 01' },
{ id: 2, comment_id: 3, title: 'Title 02' }
],
extraComments: [
{ id: 1, title: 'Title 01' },
Expand Down Expand Up @@ -169,8 +172,8 @@ describe('feature/repository/delete_with_relations', () => {
2: { id: 2, userId: 1, title: 'Title 02' }
},
comments: {
3: { id: 3, postId: 1, title: 'Title 03' },
4: { id: 4, postId: 1, title: 'Title 04' }
'[3,3]': { id: 3, comment_id: 3, postId: 1, title: 'Title 03' },
'[4,3]': { id: 4, comment_id: 3, postId: 1, title: 'Title 04' }
},
extraComments: {
3: { id: 3, postId: 1, title: 'Title 03' },
Expand All @@ -187,6 +190,7 @@ describe('feature/repository/delete_with_relations', () => {
})

it('works with morph relations', () => {
Model.clearRegistries()
class Comment extends Model {
static entity = 'comments'

Expand Down