- Table names
- Field count (number of PK columns)
- Column names (comma-separated for composite keys)
- PK definitions
Database Editions
ChartDB supports multiple SQLite editions:- Standard SQLite: File-based embedded database
- Cloudflare D1: SQLite-based edge database with Wrangler CLI integration
Smart Query Import
The SQLite Smart Query uses PRAGMA functions and json_group_array for metadata extraction.Standard SQLite Query
Cloudflare D1 Query
For Cloudflare D1, the query filters out internal_cf_* tables:
Cloudflare D1 databases can be queried using the Wrangler CLI:
What the Smart Query Retrieves
Foreign Keys (fk_info)
Foreign Keys (fk_info)
- Table and column names (schemas are always empty)
- Generated foreign key names (SQLite doesn’t name them)
- Referenced table and column
- ON UPDATE and ON DELETE actions
- FK definitions from
pragma_foreign_key_list()
Primary Keys (pk_info)
Primary Keys (pk_info)
Columns (cols)
Columns (cols)
- Data types (extracted from type definitions)
- Character maximum length (parsed from VARCHAR(n))
- Numeric precision and scale (parsed from DECIMAL(p,s))
- Ordinal position (column ID)
- Nullable constraints (from “notnull” flag)
- Default values (from dflt_value)
- AUTOINCREMENT detection (checks if column is PK INTEGER or has AUTOINCREMENT keyword)
Indexes (indexes_metadata)
Indexes (indexes_metadata)
- Index names from
pragma_index_list() - Indexed columns from
pragma_index_info() - Index type (always B-TREE for SQLite)
- Unique constraint flags
- Column positions
- Cardinality and size (not available in SQLite)
Tables (tbls)
Tables (tbls)
- Table names from
sqlite_master - Type (table or view)
- Row counts (always -1 as SQLite doesn’t track this)
- Engine and collation (empty for SQLite)
Views
Views
- View names from
sqlite_masterwhere type=‘view’
SQL Import
ChartDB can parse SQLite DDL scripts with SQLite-specific syntax.Supported Features
- Table Creation: CREATE TABLE with IF NOT EXISTS
- Data Types: SQLite’s type affinity system:
- INTEGER
- TEXT
- REAL
- BLOB
- NUMERIC
- Plus type aliases (VARCHAR, INT, etc.)
- Constraints:
- PRIMARY KEY (inline and table-level)
- FOREIGN KEY (table-level only, requires PRAGMA foreign_keys=ON)
- UNIQUE constraints
- CHECK constraints
- NOT NULL
- AUTOINCREMENT: Only works with INTEGER PRIMARY KEY
- Indexes: CREATE INDEX and CREATE UNIQUE INDEX
- Views: CREATE VIEW
- Triggers: CREATE TRIGGER (parsed but not imported)
Import Example
SQL Export
Export your ChartDB diagrams to SQLite-compatible SQL.Export Features
- PRAGMA foreign_keys: Automatically enabled if foreign keys exist
- Transaction Wrapper: BEGIN TRANSACTION / COMMIT for atomic imports
- Data Type Mapping: Converts types to SQLite equivalents:
- boolean → INTEGER (0/1)
- varchar/char → TEXT
- uuid → TEXT
- jsonb/json → TEXT
- Arrays → TEXT (JSON representation)
- datetime2/timestamp → TEXT
- INTEGER PRIMARY KEY: Special handling for AUTOINCREMENT
- Inline Foreign Keys: Foreign keys in CREATE TABLE (not ALTER TABLE)
- Double-Quote Identifiers: Proper “identifier” quoting
- Check Constraints: Full support
- Indexes: Separate CREATE INDEX statements with IF NOT EXISTS
Export Example
SQLite-Specific Features
Type Affinity
SQLite uses type affinity instead of strict types:- INTEGER: Signed integers
- TEXT: Text strings
- REAL: Floating point
- BLOB: Binary data
- NUMERIC: Can store any type
AUTOINCREMENT
AUTOINCREMENT only works with INTEGER PRIMARY KEY:Foreign Keys
Foreign keys must be:- Enabled with
PRAGMA foreign_keys = ON; - Defined as table-level constraints (not inline)
- In the same table creation statement (no ALTER TABLE ADD FOREIGN KEY)
No ALTER TABLE for Constraints
SQLite doesn’t support adding foreign keys with ALTER TABLE. All constraints must be in CREATE TABLE.Schemas
SQLite doesn’t support schemas (namespaces). If importing from a multi-schema database, ChartDB will:- Add schema names as comments
- Use only table names in the SQL
JSON Support
SQLite has JSON functions but no JSON data type. Store JSON as TEXT:Cloudflare D1 Integration
Cloudflare D1 is SQLite at the edge. Special considerations:Wrangler CLI Import
ChartDB provides a Wrangler command wrapper:System Table Filtering
D1 uses_cf_* tables for internal state. The D1 Smart Query filters these out:
Limitations
D1 has some limitations compared to standard SQLite:- No ATTACH DATABASE
- Limited PRAGMA support
- Size limits (10MB in free tier)
Best Practices
Use INTEGER PRIMARY KEY for auto-increment
This is the only way to get auto-incrementing IDs in SQLite
Troubleshooting
Foreign keys not enforced
Foreign keys not enforced
Run
PRAGMA foreign_keys = ON; before creating tables. This setting is per-connection.AUTOINCREMENT not working
AUTOINCREMENT not working
AUTOINCREMENT only works with
INTEGER PRIMARY KEY, not BIGINT or other integer types.ALTER TABLE ADD FOREIGN KEY fails
ALTER TABLE ADD FOREIGN KEY fails
SQLite doesn’t support adding foreign keys after table creation. Recreate the table instead.
Type mismatch errors
Type mismatch errors
SQLite is flexible with types. Ensure you’re using compatible type affinities.
D1 query fails
D1 query fails
Ensure you have:
- Wrangler CLI installed and logged in
- Correct database name and ID in wrangler.toml
- Proper escaping of the query for CLI
