What is SQL to Haskell Persistent Model Generator?
Persistent is Haskell's most widely used database library, forming the data layer of the Yesod web framework. It provides a type-safe, multi-backend ORM with automatic migrations, compile-time schema verification via Template Haskell, and support for PostgreSQL, MySQL, SQLite, and MongoDB. This converter takes your SQL CREATE TABLE statements and generates Persistent entity definitions using the quasi-quoted persistLowerCase DSL, including proper Haskell type mappings, Maybe for nullable columns, EntityId types for foreign keys, and optional ToJSON/FromJSON instance derivation for API serialization.
How to Use
- Paste your SQL CREATE TABLE statements into the input panel
- Toggle "Include deriving clause" to add deriving Show Eq (and Generic if JSON is enabled) to each entity
- Toggle "Include JSON instances" to generate deriveJSON Template Haskell calls for automatic ToJSON/FromJSON
- Click "Generate Persistent Models" and copy the Haskell code into your Yesod models file
- Ensure your .cabal file includes persistent, persistent-template, and your chosen backend package
Why Use This Tool?
Tips & Best Practices
- Persistent uses persistLowerCase by default, converting PascalCase entity names to snake_case table names automatically
- The Id sql= syntax is essential for auto-increment columns — it tells Persistent the SQL column name while generating a typed Key
- Enable JSON instances when building REST APIs — deriveJSON produces ToJSON/FromJSON instances that serialize entity records
- Include persistent, persistent-template, and persistent-postgresql (or your backend) in your .cabal file dependencies
Frequently Asked Questions
How are SQL types mapped to Haskell types in Persistent?
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, TIME maps to TimeOfDay, REAL/FLOAT/DOUBLE map to Double, BLOB/BYTEA map to ByteString, UUID maps to Text, JSON/JSONB map to Value (from Data.Aeson). Nullable columns wrap the type with Maybe.
When should I avoid using Persistent?
Persistent is tightly coupled to the Yesod ecosystem and its Template Haskell approach. Avoid it if you prefer a simpler library like Beam or Hasql for type-safe SQL without TH, if you need complex SQL that Persistent cannot express, or if the compile-time overhead of TH is problematic for your development workflow.
What is the Persistent entity syntax?
Persistent uses a quasi-quoted DSL inside [persistLowerCase| ... |]. Each entity is defined with its name followed by indented field definitions: fieldName FieldType with optional modifiers like Maybe for nullable, sql= for explicit column names, and deriving clauses. Template Haskell processes this DSL to generate data types and database operations.
Can I use this with Yesod?
Yes. The generated Persistent entity definitions are fully compatible with Yesod. Paste them directly into your models file (typically config/models.persistentmodels or Model.hs) and they integrate with the Yesod scaffolded site structure, including route generation and form handling.
Is my SQL data sent to a server?
No. All parsing and code generation runs entirely in your browser. Your SQL schema is never transmitted over the network, and no external services are contacted during the conversion process.
Real-world Examples
Yesod web application with Persistent models
A Haskell developer building a Yesod web application pastes their PostgreSQL schema into this tool, generates Persistent entity definitions with deriving and JSON instances, and places the output in their models file. Yesod automatically generates CRUD handlers and forms from the entity definitions.
CREATE TABLE articles ( id SERIAL PRIMARY KEY, title VARCHAR(255) NOT NULL, body TEXT, author_id INTEGER NOT NULL REFERENCES users(id), published BOOLEAN DEFAULT FALSE, created_at TIMESTAMP DEFAULT NOW() );
Article
title Text
body Text Maybe
authorId UserId
published Bool default=False
createdAt UTCTime default=NOW()
deriving Show EqREST API with JSON serialization
A Haskell servant API project enables the JSON instances option to get deriveJSON calls for each entity, enabling automatic serialization of Persistent records to JSON responses without manual ToJSON instance definitions.