Skip to content

Question about type overrides in SQLite #316

@janbiehl

Description

@janbiehl

Hello, I have a question about overriding types. For example, if I want to store a time in SQLite, I can do so either as TEXT or as INTEGER. I'm not sure if I understand type overrides correctly, but shouldn't it be possible to say that the column created_at, for example, is interpreted as a DateTime?

Together with Dapper, i could then introduce a TypeHandler, if necessary, to handle the respective case.

Currently, I don't see that overrides work for this, am i wrong or is it just not supported right now? As said i tried it for SQLite, and it remains generated as required string.

CREATE TABLE users (
    id INTEGER PRIMARY KEY,
    email TEXT NOT NULL UNIQUE,
    password_hash TEXT NOT NULL,
    display_name TEXT NOT NULL,
    is_active INTEGER NOT NULL DEFAULT 1 CHECK (is_active IN (0, 1)),
    is_deleted INTEGER NOT NULL DEFAULT 0 CHECK (is_deleted IN (0, 1)),
    last_login_at TEXT,
    created_at TEXT NOT NULL DEFAULT (datetime('now')),
    updated_at TEXT NOT NULL DEFAULT (datetime('now'))
);

sqlc.yaml

version: "2"
sql:
  - engine: "sqlite"
    schema: "database/schema.sql"
    queries: "database/queries.sql"
    codegen:
      - plugin: "csharp"
        out: "SmartStorage.Data"
        options:
          namespace: database
          useDapper: true
          overrides:
            - column: "*:created_at"
              csharp_type:
                type: "DateTime"
                notNull: true
          
plugins:
  - name: csharp
    wasm:
      url: https://github.com/DaredevilOSS/sqlc-gen-csharp/releases/download/v0.21.0/sqlc-gen-csharp.wasm
      sha256: 6a01b8c24418abff5d3c272dd1b4ad69f2b43939bb8866243947db3d167a079c

The generated model

public class User
{
    public required int Id { get; init; }
    public required string Email { get; init; }
    public required string PasswordHash { get; init; }
    public required string DisplayName { get; init; }
    public required int IsActive { get; init; }
    public required int IsDeleted { get; init; }
    public string? LastLoginAt { get; init; }
    public required string CreatedAt { get; init; }
    public required string UpdatedAt { get; init; }
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions