drizzle-orm icon indicating copy to clipboard operation
drizzle-orm copied to clipboard

[BUG]: Geometry type ignores SRID option

Open matthiasfeist opened this issue 1 year ago • 1 comments

What version of drizzle-orm are you using?

0.32.0

What version of drizzle-kit are you using?

0.23.0

Describe the Bug

The new Postgis geometry type has an option for the SRID but never passes it into the field definition SQL here and here.

This is similar to issue #2454 where the geometry type also never passes the "type" to the SQL field definition.

Expected behavior

For a schema entry like this

geom: geometry("geom", {
      type: "point",
      mode: "xy",
      srid: 4326,
    }).notNull(),

I would expect a SQL field definition like this: geometry(point, 4326)

Environment & setup

No response

matthiasfeist avatar Jul 22 '24 06:07 matthiasfeist

I opened a PR fixing this https://github.com/drizzle-team/drizzle-orm/pull/2774

mauriciabad avatar Aug 09 '24 14:08 mauriciabad

This bug essentially makes the geometry type unusable. To work around this I need to use the raw sql insertion from the docs: 'sqlST_SetSRID(ST_MakePoint(-90.9, 18.7), 4326),'

IMO this workaround kinda defeats the purpose of using an ORM. I see that there is a PR open to fix this. Can this be prioritized for the next release?

jbccollins avatar Oct 24 '24 05:10 jbccollins

This bug essentially makes the geometry type unusable. To work around this I need to use the raw sql insertion from the docs: 'sqlST_SetSRID(ST_MakePoint(-90.9, 18.7), 4326),'

IMO this workaround kinda defeats the purpose of using an ORM. I see that there is a PR open to fix this. Can this be prioritized for the next release?

+1 this is waiting for merge. LGTM! @L-Mario564

MauriceArikoglu avatar Nov 27 '24 10:11 MauriceArikoglu

ran into this too. i've temporarily worked around this issue using a custom migration:

-- Custom SQL migration file, put you code below! --
ALTER TABLE places
  ALTER COLUMN location TYPE geometry(Point, 4326) USING ST_SetSRID(location, 4326);
--> statement-breakpoint
ALTER TABLE cities
  ALTER COLUMN location TYPE geometry(Point, 4326) USING ST_SetSRID(location, 4326);

iamdavidmartin avatar Dec 06 '24 16:12 iamdavidmartin

Also running into this issue, it would be great if pull request #2774 could be merged

samhornstein avatar Jan 19 '25 17:01 samhornstein

Running into this issue too.

stuyy avatar Mar 30 '25 03:03 stuyy

me too. don't take this lightly

mikelpr avatar Jun 17 '25 00:06 mikelpr

Still an issue today...

Ryguy10712 avatar Jun 26 '25 23:06 Ryguy10712

Unfortunately , This still is an issue , to anyone who is still facing this in 2025, here is a very simple solution for you .

//From Official Package
//utils.ts 
function hexToBytes(hex: string): Uint8Array {
	const bytes: number[] = [];
	for (let c = 0; c < hex.length; c += 2) {
		bytes.push(Number.parseInt(hex.slice(c, c + 2), 16));
	}
	return new Uint8Array(bytes);
}

function bytesToFloat64(bytes: Uint8Array, offset: number): number {
	const buffer = new ArrayBuffer(8);
	const view = new DataView(buffer);
	for (let i = 0; i < 8; i++) {
		view.setUint8(i, bytes[offset + i]!);
	}
	return view.getFloat64(0, true);
}

export function parseEWKB(hex: string): [number, number] {
	const bytes = hexToBytes(hex);

	let offset = 0;

	// Byte order: 1 is little-endian, 0 is big-endian
	const byteOrder = bytes[offset];
	offset += 1;

	const view = new DataView(bytes.buffer);
	const geomType = view.getUint32(offset, byteOrder === 1);
	offset += 4;

	let _srid: number | undefined;
	if (geomType & 0x20000000) { // SRID flag
		_srid = view.getUint32(offset, byteOrder === 1);
		offset += 4;
	}

	if ((geomType & 0xFFFF) === 1) {
		const x = bytesToFloat64(bytes, offset);
		offset += 8;
		const y = bytesToFloat64(bytes, offset);
		offset += 8;

		return [x, y];
	}

	throw new Error('Unsupported geometry type');
}
//myCustomType.ts
import { customType } from 'drizzle-orm/pg-core';
import { parseEWKB } from './utils';

export const customGeometry = customType<{
	data:[number,number],
	driverData:string
}>({
    dataType(config) {
		return 'geometry(point,4326)';
	},
	fromDriver(value) {
		const [x,y] = parseEWKB(value);
		return [x,y];
	},
	toDriver(value:[number,number]) {
		return `point(${value[0]} ${value[1]})`;
	},
})
    

It would be ideal for them team to come up with a proper solution , but until then , this would be a great workaround for ppl who wanna make things work

anasmohammed361 avatar Aug 15 '25 02:08 anasmohammed361

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 srid issue 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 up for 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:

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!

AndriiSherman avatar Aug 30 '25 18:08 AndriiSherman