Skip to content

Panics on array_depth: 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 #20

@Blankeos

Description

@Blankeos

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

  1. 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:
    docker compose up -d
    // 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
  2. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions