Skip to content
This repository was archived by the owner on Mar 1, 2026. It is now read-only.

Commit aa381ab

Browse files
committed
fix handling of a single column unique index with externalIdMapping
1 parent c7ad7d7 commit aa381ab

2 files changed

Lines changed: 8 additions & 4 deletions

File tree

packages/server/src/api/rest/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1344,7 +1344,7 @@ export class RestApiHandler<Schema extends SchemaDef = SchemaDef> implements Api
13441344
if (name === externalIdName) {
13451345
if (typeof info.type === 'string') {
13461346
// single unique field
1347-
return [this.requireField(model, info.type)];
1347+
return [this.requireField(model, name)];
13481348
} else {
13491349
// compound unique fields
13501350
return Object.keys(info).map((f) => this.requireField(model, f));

packages/server/test/api/rest.test.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3183,6 +3183,7 @@ describe('REST server tests', () => {
31833183
model Post {
31843184
id Int @id @default(autoincrement())
31853185
title String
3186+
short_title String @unique()
31863187
author User? @relation(fields: [authorId], references: [id])
31873188
authorId Int?
31883189
}
@@ -3195,6 +3196,7 @@ describe('REST server tests', () => {
31953196
endpoint: 'http://localhost/api',
31963197
externalIdMapping: {
31973198
User: 'name_source',
3199+
Post: 'short_title',
31983200
},
31993201
});
32003202
handler = (args) => _handler.handleRequest({ ...args, url: new URL(`http://localhost/${args.path}`) });
@@ -3204,7 +3206,9 @@ describe('REST server tests', () => {
32043206
await client.user.create({
32053207
data: { id: 1, name: 'User1', source: 'a' },
32063208
});
3207-
3209+
await client.post.create({
3210+
data: { id: 2, title: 'Some title', short_title: 'post-title-2', authorId: 1 },
3211+
});
32083212
// user is no longer exposed using the `id` field
32093213
let r = await handler({
32103214
method: 'get',
@@ -3229,13 +3233,13 @@ describe('REST server tests', () => {
32293233
expect(r.body.data.attributes.name).toBe('User1');
32303234

32313235
await client.post.create({
3232-
data: { id: 1, title: 'Title1', authorId: 1 },
3236+
data: { id: 1, title: 'Title1', short_title: 'post-title-1', authorId: 1 },
32333237
});
32343238

32353239
// post is exposed using the `id` field
32363240
r = await handler({
32373241
method: 'get',
3238-
path: '/post/1',
3242+
path: '/post/post-title-1',
32393243
query: { include: 'author' },
32403244
client,
32413245
});

0 commit comments

Comments
 (0)