I'm getting this error:
Result::unwrap()` on an `Err` value: ColumnDecode { index: "\"array_depth\"", source: "mismatched types; Rust type `i16` (as SQL type `INT2`) is not compatible with SQL type `INT4`
This indicates a type mismatch when sql-gen attempts to decode the array_depth column during database introspection for PostgreSQL. Specifically, the Rust code expects an i16 (which sqlx maps to INT2 in SQL), but the PostgreSQL database returns an INT4 (a 32-bit integer) for the array_depth
Reproduction steps
-
Set up a PostgreSQL database.
This was my particular setup
# docker-compose.yml
version: "3.8"
name: sqlgenrs # Container name
services:
db:
container_name: sqlgenrs_db # Subcontainer name
image: postgres:15
environment:
POSTGRES_DB: sqlgenrs_db
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password123
ports:
- "5432:5432"
volumes:
- db_data:/var/lib/postgresql/data
# The database URI for connecting to this service from another service would be:
# postgresql://postgres:password123@sqlgenrs_db:5432/sqlgenrs_db
# From outside Docker (e.g., your local machine), it would be:
# postgresql://postgres:password123@localhost:5432/sqlgenrs_db
volumes:
db_data:
// I used this particular prisma schema store this in
// prisma/schema.prisma
model User {
id String @id
username String
passwordHash String
createdTimestamp DateTime @default(now())
updatedTimestamp DateTime @updatedAt @default(now())
sessions Session[]
}
model Session {
id String @id
userId String
expiresAt DateTime
user User @relation(references: [id], fields: [userId], onDelete: Cascade)
}
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
// Looking for ways to speed up your queries, or scale easily with your serverless or edge functions?
// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init
generator client {
provider = "prisma-client-js"
output = "../src/generated/prisma"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
Store in your .env
DATABASE_URL="postgresql://postgres:password123@localhost:5432/sqlgenrs_db"
Then just run:
npx prisma migrate dev --schema ./prisma
-
Run the sql-gen command, for example:
sql-gen --db-url postgresql://postgres:password123@localhost:5432/sqlgenrs_db --output src/models/
Possible causes
In the sql-gen codebase, the PostgresTableColumn struct, defined in sql-gen/src/postgres/models/postgres_table_column.rs, has its array_depth field incorrectly defined as i16:
// sql-gen/src/postgres/models/postgres_table_column.rs#L11
pub array_depth: i16,
This leads to a decoding error because sqlx attempts to cast the INT4 value from the database into a Rust i16 (INT2), resulting in the "mismatched types" error.
Maybe it should be i32? Not i16? I can try doing this but idk if this change is severe and might affect mysql, etc.
Edit: Just confirmed in #21 it works when I changed it.
I'm getting this error:
This indicates a type mismatch when
sql-genattempts to decode thearray_depthcolumn during database introspection for PostgreSQL. Specifically, the Rust code expects ani16(whichsqlxmaps toINT2in SQL), but the PostgreSQL database returns anINT4(a 32-bit integer) for thearray_depthReproduction steps
Set up a PostgreSQL database.
This was my particular setup
Store in your .env
DATABASE_URL="postgresql://postgres:password123@localhost:5432/sqlgenrs_db"Then just run:
Run the sql-gen command, for example:
Possible causes
In the
sql-gencodebase, thePostgresTableColumnstruct, defined insql-gen/src/postgres/models/postgres_table_column.rs, has itsarray_depthfield incorrectly defined asi16:This leads to a decoding error because
sqlxattempts to cast theINT4value from the database into a Rusti16(INT2), resulting in the "mismatched types" error.Maybe it should be
i32? Noti16? I can try doing this but idk if this change is severe and might affect mysql, etc.Edit: Just confirmed in #21 it works when I changed it.