drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
23.44k stars 574 forks source link

[BUG]: "Offset is outside the bounds of the DataView" when querying PgGeometry field via RQB relation #2788

Open Sukairo-02 opened 1 month ago

Sukairo-02 commented 1 month ago

What version of drizzle-orm are you using?

0.33.0

What version of drizzle-kit are you using?

0.24.0

Describe the Bug

Getting Offset is outside the bounds of the DataView error thrown while querying geometry fields via RQB relations

Expected behavior

Expected to get my data returned and not an error thrown

Environment & setup

Schema:

import { relations } from 'drizzle-orm';
import {
    boolean,
    char,
    date,
    geometry,
    integer,
    pgEnum,
    pgTable,
    serial,
    text,
    timestamp,
    varchar,
    vector,
} from 'drizzle-orm/pg-core';

export const roleEnum = pgEnum('role', ['admin', 'user']);

export const Users = pgTable('users', {
    a: integer('a').array(),
    id: serial('id').primaryKey(),
    name: text('name').notNull(),
    email: text('email'),
    birthdayString: date('birthday_string', { mode: 'string' }),
    birthdayDate: date('birthday_date', { mode: 'date' }),
    createdAt: timestamp('created_at').notNull().defaultNow(),
    role: roleEnum('role'),
    roleText: text('role1', { enum: ['admin', 'user'] }),
    roleText2: text('role2', { enum: ['admin', 'user'] }).default('user'),
    profession: varchar('profession', { length: 20 }),
    initials: char('initials', { length: 2 }),
    isConfirmed: boolean('is_confirmed'),
    vector: vector('vector_column', { dimensions: 5 }),
    geoXy: geometry('geometry_xy', {
        mode: 'xy',
    }),
    geoTuple: geometry('geometry_tuple', {
        mode: 'tuple',
    }),
});

export const Customers = pgTable('customers', {
    id: serial('id').primaryKey(),
    address: text('address').notNull(),
    isConfirmed: boolean('is_confirmed'),
    registrationDate: timestamp('registration_date').notNull().defaultNow(),
    userId: integer('user_id')
        .references(() => Users.id)
        .notNull(),
});

export const Posts = pgTable('posts', {
    id: serial('id').primaryKey(),
    content: text('content'),
    authorId: integer('author_id'),
});

export const usersRelations = relations(Users, ({ one, many }) => ({
    posts: many(Posts),
    customer: one(Customers, {
        fields: [Users.id],
        references: [Customers.userId],
    }),
}));

export const customersRelations = relations(Customers, ({ one, many }) => ({
    user: one(Users, {
        fields: [Customers.userId],
        references: [Users.id],
    }),
    posts: many(Posts),
}));

export const postsRelations = relations(Posts, ({ one }) => ({
    author: one(Users, {
        fields: [Posts.authorId],
        references: [Users.id],
    }),
    customer: one(Customers, {
        fields: [Posts.authorId],
        references: [Customers.userId],
    }),
}));

Database docker image: joshuasundance/postgis_pgvector

Query:

        await db.query.Posts.findFirst({
            columns: {
                authorId: true,
                content: true,
                id: true,
            },
            with: {
                author: {
                    columns: {
                        a: true,
                        birthdayDate: true,
                        birthdayString: true,
                        createdAt: true,
                        email: true,
                        geoTuple: true, // Remove this
                        geoXy: true, // and this - then error does not occur
                        id: true,
                        initials: true,
                        isConfirmed: true,
                        name: true,
                        profession: true,
                        role: true,
                        roleText: true,
                        roleText2: true,
                        vector: true,
                    },
                },
            },
        });

Full error:

/home/sergey/job/drizzle-graphql/node_modules/.pnpm/drizzle-orm@0.33.0_@libsql+client@0.5.6_@types+pg@8.11.6_mysql2@3.9.7_pg@8.12.0_postgres@3.4.4/node_modules/src/pg-core/columns/postgis_extension/utils.ts:28
        const geomType = view.getUint32(offset, byteOrder === 1);
                              ^

RangeError: Offset is outside the bounds of the DataView
    at DataView.getUint32 (<anonymous>)
    at parseEWKB (/home/sergey/job/drizzle-graphql/node_modules/.pnpm/drizzle-orm@0.33.0_@libsql+client@0.5.6_@types+pg@8.11.6_mysql2@3.9.7_pg@8.12.0_postgres@3.4.4/node_modules/src/pg-core/columns/postgis_extension/utils.ts:28:24)
    at Proxy.mapFromDriverValue (/home/sergey/job/drizzle-graphql/node_modules/.pnpm/drizzle-orm@0.33.0_@libsql+client@0.5.6_@types+pg@8.11.6_mysql2@3.9.7_pg@8.12.0_postgres@3.4.4/node_modules/src/pg-core/columns/postgis_extension/geometry.ts:46:10)
    at mapRelationalRow (/home/sergey/job/drizzle-graphql/node_modules/.pnpm/drizzle-orm@0.33.0_@libsql+client@0.5.6_@types+pg@8.11.6_mysql2@3.9.7_pg@8.12.0_postgres@3.4.4/node_modules/src/relations.ts:713:66)
    at mapRelationalRow (/home/sergey/job/drizzle-graphql/node_modules/.pnpm/drizzle-orm@0.33.0_@libsql+client@0.5.6_@types+pg@8.11.6_mysql2@3.9.7_pg@8.12.0_postgres@3.4.4/node_modules/src/relations.ts:686:9)
    at <anonymous> (/home/sergey/job/drizzle-graphql/node_modules/.pnpm/drizzle-orm@0.33.0_@libsql+client@0.5.6_@types+pg@8.11.6_mysql2@3.9.7_pg@8.12.0_postgres@3.4.4/node_modules/src/pg-core/query-builders/query.ts:101:7)
    at Array.map (<anonymous>)
    at <anonymous> (/home/sergey/job/drizzle-graphql/node_modules/.pnpm/drizzle-orm@0.33.0_@libsql+client@0.5.6_@types+pg@8.11.6_mysql2@3.9.7_pg@8.12.0_postgres@3.4.4/node_modules/src/pg-core/query-builders/query.ts:100:27)
    at <anonymous> (/home/sergey/job/drizzle-graphql/node_modules/.pnpm/drizzle-orm@0.33.0_@libsql+client@0.5.6_@types+pg@8.11.6_mysql2@3.9.7_pg@8.12.0_postgres@3.4.4/node_modules/src/node-postgres/session.ts:77:8)
    at Object.startActiveSpan (/home/sergey/job/drizzle-graphql/node_modules/.pnpm/drizzle-orm@0.33.0_@libsql+client@0.5.6_@types+pg@8.11.6_mysql2@3.9.7_pg@8.12.0_postgres@3.4.4/node_modules/src/tracing.ts:27:11)

Node.js v21.7.1
ZarkoPernar commented 1 month ago

I have exactly the same issue in my local supabase. I also tried converting the column from geometry to geography with an sql script and the error went away. From what I can see, drizzle doesn't support defining columns as geography which is even more puzzling as to why this would fix the issue.