Overview
Gitea uses XORM as its ORM (Object-Relational Mapping) layer, providing database-agnostic access to PostgreSQL, MySQL, SQLite, and MSSQL.Supported Databases
PostgreSQL
Recommended for production
- Version 12+
- Best performance
- Full feature support
MySQL / MariaDB
Production-ready alternative
- MySQL 8.0+
- MariaDB 10.4+
- utf8mb4 charset required
SQLite
Development and small deployments
- Version 3.35+
- Single-file database
- No separate server needed
MSSQL
Enterprise environments
- SQL Server 2012+
- Windows-native integration
XORM Basics
Database Engine
Access the database through the engine:Common Operations
- Insert
- Select
- Update
- Delete
Model Definition
Basic Model
XORM Tags
Common XORM struct tags:| Tag | Description | Example |
|---|---|---|
pk | Primary key | xorm:"pk" |
autoincr | Auto-increment | xorm:"autoincr" |
UNIQUE | Unique constraint | xorm:"UNIQUE" |
NOT NULL | Not null constraint | xorm:"NOT NULL" |
DEFAULT | Default value | xorm:"DEFAULT true" |
INDEX | Create index | xorm:"INDEX" |
created | Auto-set on insert | xorm:"created" |
updated | Auto-set on update | xorm:"updated" |
deleted | Soft delete | xorm:"deleted" |
- | Ignore field | xorm:"-" |
Indexes
Query Builders
Find Options Pattern
Gitea uses a Find Options pattern for complex queries:Pagination
Transactions
Basic Transaction
Migrations
Migration Files
Migrations are located inmodels/migrations/:
Creating a Migration
Register Migration
Migration Best Practices
Test All Databases
Test All Databases
Ensure migrations work on PostgreSQL, MySQL, SQLite, and MSSQL:
Handle Data Migration
Handle Data Migration
When changing schema, migrate existing data:
Use Transactions
Use Transactions
Wrap multi-step migrations in transactions when possible
Database Utilities
Count Records
Check Existence
Batch Operations
Performance Optimization
Eager Loading
Indexes
Add indexes for frequently queried columns:See Also
Architecture
Overall architecture overview
Database Setup
Production database configuration
Testing
Writing database tests