drizzle-orm
drizzle-orm copied to clipboard
[BUG]: findFirst and findMany queries using the 'with' statement can't parse the models if the related table has a geomtery column
What version of drizzle-orm are you using?
0.32.1
What version of drizzle-kit are you using?
0.23.0
Describe the Bug
Using the 'with' statement in the findFirst and findMany queries results in this error "RangeError: Offset is outside the bounds of the DataView" if the related table has a geometry column.
Steps to reproduce: Two tables with the following relationship
export const reviews = createTable('reviews', {
id: serial('id').primaryKey().notNull(),
createdAt: timestamp('created_at', { withTimezone: true })
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
updatedAt: timestamp('updated_at', { withTimezone: true }).default(sql`CURRENT_TIMESTAMP`),
draft: boolean('draft').default(true).notNull(),
hotelId: integer('hotel_id')
.references(() => hotels.id, { onDelete: 'cascade' })
.notNull(),
});
export const reviewsRelations = relations(reviews, ({ one }) => ({
hotel: one(hotels, { fields: [reviews.hotelId], references: [hotels.id] }),
}))
export const hotels = createTable(
'hotels',
{
id: serial('id').primaryKey(),
name: text('name').notNull(),
createdAt: timestamp('created_at', { withTimezone: true })
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
updatedAt: timestamp('updated_at', { withTimezone: true }).default(sql`CURRENT_TIMESTAMP`),
location: geometry('location', { type: 'point', mode: 'xy', srid: 4326 })
},
(hotel) => ({
spatialIndex: index('spatial_index').using('gist', hotel.location),
}),
);
export const hotelRelations = relations(hotels, ({ many, one }) => ({
reviews: many(reviews),
}));
running the following queries should result in the error stated above.
const reviews = await db.query.reviews.findMany({
where: and(eq(reviews.draft, false)),
with: {
hotel: true,
},
});
const review = await db.query.reviews.findFirst({
where: and(eq(reviews.id, 320)),
with: {
hotel: true,
},
});
It appears that the generated SQL for both the findFirst and findMany queries works fine
-- findFirst
select "reviews"."id", "reviews"."created_at", "reviews"."updated_at", "reviews"."hotel_id", "reviews"."draft", "reviews_hotel"."data" as "hotel" from "reviews" "reviews" left join lateral (select json_build_array("reviews_hotel"."id", "reviews_hotel"."name", "reviews_hotel"."created_at", "reviews_hotel"."updated_at", "reviews_hotel"."location", "reviews_hotel"."rating") as "data" from (select * from "hotels" "reviews_hotel" where "reviews_hotel"."id" = "reviews"."hotel_id" limit 1) "reviews_hotel") "reviews_hotel" on true where "reviews"."id" = 320 limit 1
-- findMany
select "reviews"."id", "reviews"."created_at", "reviews"."updated_at", "reviews"."hotel_id", "reviews"."draft", "reviews_hotel"."data" as "hotel" from "reviews" "reviews" left join lateral (select json_build_array("reviews_hotel"."id", "reviews_hotel"."name", "reviews_hotel"."created_at", "reviews_hotel"."updated_at", "reviews_hotel"."location") as "data" from (select * from "hotels" "reviews_hotel" where "reviews_hotel"."id" = "reviews"."hotel_id" limit 1) "reviews_hotel") "reviews_hotel" on true where "reviews"."draft" = false
It works works fine if I use the standard select method with a leftJoin on the hotels table.
const reviewResults = await db
.select()
.from(reviews)
.leftJoin(hotels, eq(hotels.id, reviews.hotelId))
.where(and(eq(reviews.draft, false)))
.limit(6)
.orderBy(desc(reviews.updatedAt));
It appears it could be a parsing issue with the findFirst and findMany queries if using the 'with' statement on a table with a geometry column.
Expected behavior
Both the findFirst and findMany queries should return the review object and related hotel object correctly.
Environment & setup
Next.js project using a Supabase postgres DB with the PostGIS extension enabled.
Same issue here. If you don't need the geometry column in your context, you can explicitly omit it. For the example above, it would look something like:
const reviews = await db.query.reviews.findMany({
with: { hotel: { columns: { id: true, name: true } } },
});
Thanks @masons40 for providing the workaround using db.select().
Hey everyone!
I've created this message to send in a batch to all opened issues we have, just because there are a lot of them and I want to update all of you with our current work, why issues are not responded to, and the amount of work that has been done by our team over ~8 months.
I saw a lot of issues with suggestions on how to fix something while we were not responding – so thanks everyone. Also, thanks to everyone patiently waiting for a response from us and continuing to use Drizzle!
We currently have 4 major branches with a lot of work done. Each branch was handled by different devs and teams to make sure we could make all the changes in parallel.
First branch is drizzle-kit rewrite
All of the work can be found on the alternation-engine branch. Here is a PR with the work done: https://github.com/drizzle-team/drizzle-orm/pull/4439
As you can see, it has 167k added lines of code and 67k removed, which means we've completely rewritten the drizzle-kit alternation engine, the way we handle diffs for each dialect, together with expanding our test suite from 600 tests to ~9k test units for all different types of actions you can do with kit. More importantly, we changed the migration folder structure and made commutative migrations, so you won't face complex conflicts on migrations when working in a team.
What's left here:
- We are finishing handling defaults for Postgres, the last being geometry (yes, we fixed the
sridissue here as well). - We are finishing commutative migrations for all dialects.
- We are finishing up the command, so the migration flow will be as simple as
drizzle-kit upfor you.
Where it brings us:
- We are getting drizzle-kit into a new good shape where we can call it
[email protected]!
Timeline:
- We need ~2 weeks to finish all of the above and send this branch to beta for testing.
Second big branch is a complex one with several HUGE updates
- Bringing Relational Queries v2 finally live. We've done a lot of work here to actually make it faster than RQBv1 and much better from a DX point of view. But in implementing it, we had to make another big rewrite, so we completely rewrote the drizzle-orm type system, which made it much simpler and improved type performance by ~21.4x:
(types instantiations for 3300 lines production drizzle schema + 990 lines relations)
TS v5.8.3: 728.8k -> 34.1k
TS v5.9.2: 553.7k -> 25.4k
You can read more about it here.
What's left here:
- We have 1 issue with TS that is already in progress of being fixed. The issue and Post about fixing.
Where it brings us:
- We are getting drizzle-orm into a new good shape where we can call it
[email protected]!
Breaking changes:
- We will have them, but we will have open channels for everyone building on top of drizzle types, so we can guide you through all the changes.
Third branch is adding support for CockroachDB and MSSQL dialects
Support for them is already in the alternation-engine branch and will be available together with the drizzle-kit rewrite.
Summary
All of the work we are doing is crucial and should be done sooner rather than later. We've received a lot of feedback and worked really hard to find the best strategies and decisions for API, DX, architecture, etc., so we can confidently mark it as v1 and be sure we can improve it and remain flexible for all the features you are asking for, while becoming even better for everyone building on top of the drizzle API as well.
We didn't want to stay with some legacy decisions and solutions we had, and instead wanted to shape Drizzle in a way that will be best looking ahead to 2025–2026 trends (v1 will get proper effect support, etc.).
We believe that all of the effort we've put in will boost Drizzle and benefit everyone using it.
Thanks everyone, as we said, we are here to stay for a long time to build a great tool together!
Timelines
We are hoping to get v1 for drizzle in beta this fall and same timeline for latest. Right after that we can go through all of the issues and PRs and resond everyone. v1 for drizzle should close ~70% of all the bug tickets we have, so on beta release we will start marking them as closed!