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
15 changes: 13 additions & 2 deletions packages/3-targets/6-adapters/postgres/src/core/sql-renderer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import {
type WindowFuncExpr,
} from '@internal/sql-relational-core/ast';
import type { PostgresCodecDescriptorRegistry } from '@internal/target-postgres/codec-descriptor';
import { PG_ENUM_CODEC_ID } from '@internal/target-postgres/codec-ids';
import { isPgEnumParams } from '@internal/target-postgres/codecs';
import {
escapeLiteral,
Expand All @@ -48,7 +49,7 @@ import {
import { ifDefined } from '@internal/utils/defined';
import { assertNever, InternalError } from '@internal/utils/internal-error';
import { adapterError } from './adapter-errors';
import type { PostgresContract } from './types';
import type { PostgresContract, StorageColumn } from './types';

/**
* Postgres native types whose unknown-OID parameter inference is reliable in arbitrary expression positions. Parameters bound to a descriptor whose `nativeTypeFor` result falls in this set are emitted as plain `$N`; everything else (including `json`, `jsonb`, extension types like `vector`, and unknown user types) is emitted as `$N::<nativeType>` so the planner picks an unambiguous overload.
Expand Down Expand Up @@ -279,6 +280,10 @@ function allStrings(values: readonly JsonValue[]): values is readonly string[] {
return values.every((value) => typeof value === 'string');
}

function sortsByDeclarationOrderNatively(column: StorageColumn): boolean {
return column.codecId === PG_ENUM_CODEC_ID;
}

function resolveEnumOrderValues(
ref: ColumnRef,
sourcesByRef: ReadonlyMap<string, TableSourceCoordinate>,
Expand All @@ -291,7 +296,10 @@ function resolveEnumOrderValues(
const sourceNs = contract.storage.namespaces[source.namespaceId];
const column =
sourceNs !== undefined ? sourceNs.entries.table?.[source.name]?.columns[ref.column] : undefined;
const valueSet = column?.valueSet;
if (column === undefined || sortsByDeclarationOrderNatively(column)) {
return undefined;
}
const valueSet = column.valueSet;
if (valueSet === undefined) {
return undefined;
}
Expand Down Expand Up @@ -325,6 +333,9 @@ function resolveEnumOrderValuesForIdentifier(
if (matchedColumns > 1) {
return undefined;
}
if (sortsByDeclarationOrderNatively(column)) {
return undefined;
}
const valueSet = column.valueSet;
if (valueSet === undefined) {
return undefined;
Expand Down
11 changes: 11 additions & 0 deletions test/integration/test/enum-order-by/_fixture/contract.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
native_enum TicketStatus {
OPEN = "open"
CLOSED = "closed"
}

model Ticket {
id Int @id
status pg.enum(TicketStatus)
Comment thread
coderabbitai[bot] marked this conversation as resolved.

@@map("tickets")
}
Loading
Loading