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/.eslintcache

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,76 @@ describe('feature/relations/belongs_to_many_save', () => {
},
})
})

it('can handle relations to itself', () => {
class ClientRetailer extends Model {
static entity = 'client_retailers'

static primaryKey = ['retailerId', 'supplierId']

static fields () {
return {
supplierId: this.number(null),
retailerId: this.number(null),
retailerCode: this.string(null),
}
}
}
class Client extends Model {
static entity = 'clients'

static fields () {
return {
id: this.number(0),
name: this.string(null),
retailers: this.belongsToMany(Client, ClientRetailer, 'supplierId', 'retailerId'),
}
}
}
const clientRepo = useRepo(Client)

clientRepo.save([
{
id: 1,
name: 'Client 1',
retailers: [
{
id: 16,
name: 'Client 16',
pivot: {
retailerCode: '555',
},
},
{
id: 18,
name: 'Client 18',
pivot: {
retailerCode: '666',
},
},
],
},
])

assertState({
client_retailers: {
'[16,1]': { retailerId: 16, supplierId: 1, retailerCode: '555' },
'[18,1]': { retailerId: 18, supplierId: 1, retailerCode: '666' },
},
clients: {
1: {
'id': 1,
'name': 'Client 1',
},
16: {
'id': 16,
'name': 'Client 16',
},
18: {
'id': 18,
'name': 'Client 18',
},
},
})
})
})