Skip to content

Enum fields of included relations are returned as @map values instead of enum names #2926

Open

Description

@ymc9

Description

Enum fields of relations loaded with include (or a nested select) are returned in their database representation (the @map value) instead of the enum member name. Top-level fields are mapped correctly.

Reproduction

enum S {
    DRAFT  @map('draft')
    ACTIVE @map('active')
}

model User {
    id     Int    @id
    status S
    posts  Post[]
}

model Post {
    id       Int  @id
    status   S
    authorId Int
    author   User @relation(fields: [authorId], references: [id])
}
await db.user.create({ data: { id: 1, status: 'ACTIVE', posts: { create: { id: 1, status: 'DRAFT' } } } });

await db.user.findMany({ include: { posts: true } });
// actual:   [{ id: 1, status: 'ACTIVE', posts: [{ id: 1, status: 'draft', authorId: 1 }] }]
// expected: [{ id: 1, status: 'ACTIVE', posts: [{ id: 1, status: 'DRAFT', authorId: 1 }] }]

await db.post.findMany({ include: { author: true } });
// actual:   [{ id: 1, status: 'DRAFT', authorId: 1, author: { id: 1, status: 'active' } }]
// expected: [{ id: 1, status: 'DRAFT', authorId: 1, author: { id: 1, status: 'ACTIVE' } }]

Reproduces on both SQLite and PostgreSQL (dev, 3.10.1).

Cause

QueryNameMapper maps enum values back to names only for plain column selections (processEnumSelection). Nested relations are built as JSON inside a subquery, e.g. on PostgreSQL:

left join lateral (
  select coalesce(jsonb_agg(jsonb_build_object('id', "$$t2"."id", 'status', "$$t2"."status", ...)), '[]'::jsonb) as "$data"
  from "public"."Post" as "$$t2" where "$$t2"."authorId" = "User"."id"
) as "$$t1" on true

so the enum columns passed to the JSON builder are never mapped.

Possible fixes

  1. Map enum column arguments of the JSON builders in the name mapper, resolving them to fields like plain selections.
  2. Map enum values to names in the ORM's result processing (JavaScript) using the schema, for top-level and nested data alike, instead of in SQL.

Related: #2867

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    runtimeZenStack runtime related

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions