SQL to Haskell Persistent Model Generator

Convert SQL CREATE TABLE statements to Haskell Persistent entity definitions with proper type mapping, automatic migrations, and Yesod integration.

Haskell Persistent

What is SQL to Haskell Persistent Model Generator?

This tool converts SQL CREATE TABLE statements to Haskell Persistent entity definitions. Persistent is Haskell's most widely used ORM, part of the Yesod web framework ecosystem. It provides a type-safe, multi-backend ORM with automatic migrations, type-safe queries, and support for PostgreSQL, MySQL, SQLite, and MongoDB.

How to Use

  1. Paste your SQL CREATE TABLE statements
  2. Configure generation options (deriving clause, JSON instances)
  3. Click "Generate" and copy the Haskell Persistent entity definitions
  4. Paste the generated code into your Yesod models file

Why Use This Tool?

Generate type-safe Persistent entity definitions from SQL schemas
Automatic SQL to Haskell type mapping with Maybe for nullable columns
SERIAL/BIGSERIAL auto-increment columns mapped to Id sql= syntax
Foreign key references converted to EntityId types
Unique constraint generation with UniqueEntityField naming

Tips & Best Practices

  • Persistent uses persistLowerCase by default, which converts entity names to snake_case table names
  • Use the Id sql= syntax for auto-increment primary keys to control the SQL column name
  • Add deriveJSON for automatic ToJSON/FromJSON instances for API responses
  • Include the persistent, persistent-template, and persistent-sqlite (or your backend) packages in your .cabal file

Frequently Asked Questions

What is Haskell Persistent?

Persistent is Haskell's most widely used ORM, part of the Yesod web framework ecosystem. It provides a type-safe, multi-backend ORM with automatic migrations, type-safe queries, and support for PostgreSQL, MySQL, SQLite, and MongoDB.

How are SQL types mapped to Haskell types?

SQL INTEGER maps to Int, BIGINT maps to Int64, VARCHAR/TEXT map to Text, BOOLEAN maps to Bool, TIMESTAMP/DATETIME map to UTCTime, DATE maps to Day, REAL/FLOAT map to Double, DOUBLE maps to Double, BLOB/BYTEA map to ByteString, UUID maps to Text, JSON/JSONB map to Value, and nullable columns use Maybe Type.

What is the Persistent entity syntax?

Persistent uses a quasi-quoted DSL to define entities. The syntax is: TableName fieldName FieldType with optional constraints like Maybe for nullable, deriving clauses, and unique constraints. This is processed by Template Haskell to generate type-safe data types and database operations.

Can I use this with Yesod?

Yes! The generated Persistent entity definitions are fully compatible with Yesod. You can paste them directly into your models file and use them with the Yesod scaffolded site structure.

Is my SQL data sent to a server?

No, all processing happens entirely in your browser. Your SQL schema never leaves your device. The conversion is performed client-side using a regex-based parser.

Related Tools