Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Pg-kit] non-native type mismatch in drizzle-kit push command. #2887

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mtarroyo
Copy link

@mtarroyo mtarroyo commented Aug 31, 2024

An error is occurring in drizzle-kit when running on tables that have non-native data types defined by extensions such as PostGIS, for example.

If we create a table like:

CREATE TABLE public.address (
	id serial4 NOT NULL,
	city_id int4 NOT NULL,
	postal_code varchar(20) NOT NULL,
	street varchar(255) NOT NULL,
	house_number varchar(10) NULL,
	district varchar(150) NOT NULL,
	complement varchar(255) NULL,
	"location" public.geography(point, 4326) NOT NULL,
	CONSTRAINT address_pk PRIMARY KEY (id)
);

ALTER TABLE public.address ADD CONSTRAINT address_city_fk FOREIGN KEY (city_id) REFERENCES public.city(id);

And a schema like:

export const address = pgTable(
  'address',
  {
    id: serial('id').notNull(),
    cityId: integer('city_id').notNull(),
    postalCode: varchar('postal_code', { length: 20 }).notNull(),
    street: varchar('street', { length: 255 }).notNull(),
    houseNumber: varchar('house_number', { length: 10 }),
    district: varchar('district', { length: 150 }).notNull(),
    complement: varchar('complement', { length: 255 }),
    location: geography('location', { type: 'Point' }).notNull()
  },
  (table) => {
    return {
      pk: primaryKey({ name: 'address_pk', columns: [table.id] }),
      cityFk: foreignKey({
        name: 'address_city_fk',
        columns: [table.cityId],
        foreignColumns: [city.id]
      })
    }
  }

Note: the postgis implementation has been suppressed here.

Soon after, if you run the drizzle-kit push command on the above structure, the following data-loss warning will occur even if the types from the actual table and the scheme matches.

Screenshot 2024-08-31 at 12 38 26

I managed to get rid of the error by changing the following code in the drizzle-kit pgSerializer.ts file

From:

...
columnToReturn[columnName] = {
    name: columnName,
    type:
        // filter vectors, but in future we should filter any extension that was installed by user
	columnAdditionalDT === 'USER-DEFINED'
		&& !['vector', 'geometry'].includes(enumType)
		? enumType
		: columnTypeMapped,
...

To:

...
columnToReturn[columnName] = {
    name: columnName,
    type: columnTypeMapped,
...

It will also work if you add geography or the mismatched type you need in the !['vector', 'geometry'] array like:

...
!['vector', 'geometry', 'geography']
...

Fix #2886

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG]: User defined types mismatch in drizzle-kit
1 participant