Skip to content

Commit

Permalink
Merge pull request #1466 from L-Mario564/beta
Browse files Browse the repository at this point in the history
[PG]: Fix `selectDistinctOn` not working with multiple columns
  • Loading branch information
AndriiSherman authored Nov 27, 2023
2 parents aad4fe0 + b5eb2c2 commit 501e283
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion drizzle-orm/src/pg-core/dialect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ export class PgDialect {

let distinctSql: SQL | undefined;
if (distinct) {
distinctSql = distinct === true ? sql` distinct` : sql` distinct on (${sql.join(distinct.on, ', ')})`;
distinctSql = distinct === true ? sql` distinct` : sql` distinct on (${sql.join(distinct.on, sql`, `)})`;
}

const selection = this.buildSelection(fieldsList, { isSingleTable });
Expand Down
28 changes: 22 additions & 6 deletions integration-tests/tests/pg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,16 +480,18 @@ test.serial('select distinct', async (t) => {
const usersDistinctTable = pgTable('users_distinct', {
id: integer('id').notNull(),
name: text('name').notNull(),
age: integer('age').notNull()
});

await db.execute(sql`drop table if exists ${usersDistinctTable}`);
await db.execute(sql`create table ${usersDistinctTable} (id integer, name text)`);
await db.execute(sql`create table ${usersDistinctTable} (id integer, name text, age integer)`);

await db.insert(usersDistinctTable).values([
{ id: 1, name: 'John' },
{ id: 1, name: 'John' },
{ id: 2, name: 'John' },
{ id: 1, name: 'Jane' },
{ id: 1, name: 'John', age: 24 },
{ id: 1, name: 'John', age: 24 },
{ id: 2, name: 'John', age: 25 },
{ id: 1, name: 'Jane', age: 24 },
{ id: 1, name: 'Jane', age: 26 }
]);
const users1 = await db.selectDistinct().from(usersDistinctTable).orderBy(
usersDistinctTable.id,
Expand All @@ -501,10 +503,18 @@ test.serial('select distinct', async (t) => {
const users3 = await db.selectDistinctOn([usersDistinctTable.name], { name: usersDistinctTable.name }).from(
usersDistinctTable,
).orderBy(usersDistinctTable.name);
const users4 = await db.selectDistinctOn([usersDistinctTable.id, usersDistinctTable.age]).from(
usersDistinctTable
).orderBy(usersDistinctTable.id, usersDistinctTable.age)

await db.execute(sql`drop table ${usersDistinctTable}`);

t.deepEqual(users1, [{ id: 1, name: 'Jane' }, { id: 1, name: 'John' }, { id: 2, name: 'John' }]);
t.deepEqual(users1, [
{ id: 1, name: 'Jane', age: 24 },
{ id: 1, name: 'Jane', age: 26 },
{ id: 1, name: 'John', age: 24 },
{ id: 2, name: 'John', age: 25 }
]);

t.deepEqual(users2.length, 2);
t.deepEqual(users2[0]?.id, 1);
Expand All @@ -513,6 +523,12 @@ test.serial('select distinct', async (t) => {
t.deepEqual(users3.length, 2);
t.deepEqual(users3[0]?.name, 'Jane');
t.deepEqual(users3[1]?.name, 'John');

t.deepEqual(users4, [
{ id: 1, name: 'John', age: 24 },
{ id: 1, name: 'Jane', age: 26 },
{ id: 2, name: 'John', age: 25 }
]);
});

test.serial('insert returning sql', async (t) => {
Expand Down

0 comments on commit 501e283

Please sign in to comment.