Fixtures
Fixtures are JSON files that contain sample data for your application. They help you set up initial data, test data, and seed your database with predefined records.
Fixtures
Fixtures are JSON files that contain sample data for your application. They help you set up initial data, test data, and seed your database with predefined records.
What are Fixtures?
Fixtures are structured JSON files that define sample data for your doctypes. They are automatically applied when you run commands like nailgun migrate or nailgun push, ensuring your database has the necessary initial data.
File Structure
Fixtures are stored in the fixtures/ directory within each app:
apps/your-app/
├── fixtures/
│ ├── doctype_name.fixture.json
│ └── another_doctype.fixture.jsonFixture File Format
Fixture files follow the naming convention: {doctype_name}.fixture.json
For example:
zodula__User.fixture.json- for the User doctypezodula__Role.fixture.json- for the Role doctype
JSON Structure
Each fixture file contains an array of objects representing the records to be inserted:
[
{
"id": "record-1",
"name": "Sample Record 1",
"description": "This is a sample record",
"is_active": true
},
{
"id": "record-2",
"name": "Sample Record 2",
"description": "Another sample record",
"is_active": false
}
]Example: Role Fixtures
Here's a complete example of a Role fixture file:
[
{
"id": "Anonymous",
"name": "Anonymous",
"description": "The person who is not logged in"
},
{
"id": "Authenticated",
"name": "Authenticated",
"description": "The person who is logged in"
},
{
"id": "System Admin",
"name": "System Admin",
"description": "The person who has all the permissions"
},
{
"id": "Test",
"name": "Test",
"description": null
}
]When Fixtures are Applied
Fixtures are automatically applied during:
- Database Migration - When running
nailgun migrate - Push Operations - When running
nailgun push - Fresh Setup - When setting up a new environment
- Development - When starting development with sample data
Creating Fixtures
Method 1: Manual Creation
Create a new .fixture.json file in your app's fixtures/ directory:
# Navigate to your app
cd apps/your-app
# Create fixtures directory if it doesn't exist
mkdir -p fixtures
# Create a fixture file
touch fixtures/YourDoctype.fixture.jsonMethod 2: Export from Database
You can export existing data as fixtures using the API:
POST /api/action/zodula.fixtures.exports
Content-Type: application/json
{
"app": "your-app",
"doctype": "YourDoctype",
"ids": ["record-1", "record-2"],
"fields": ["name", "description"]
}Best Practices
1. Use Meaningful IDs
Use descriptive, stable IDs that won't change:
[
{
"id": "admin-role",
"name": "Administrator"
},
{
"id": "user-role",
"name": "User"
}
]2. Include Required Fields
Make sure to include all required fields for your doctype:
[
{
"id": "user-001",
"name": "John Doe",
"email": "john@example.com",
"is_active": true
}
]3. Use Consistent Data
Keep fixture data consistent and realistic:
[
{
"id": "product-001",
"name": "Laptop Computer",
"price": 999.99,
"category": "Electronics",
"in_stock": true
}
]4. Handle Relationships
When fixtures reference other records, use the same IDs:
[
{
"id": "order-001",
"customer": "customer-001",
"product": "product-001",
"quantity": 2
}
]Common Use Cases
1. System Roles
Define default user roles and permissions:
[
{
"id": "admin",
"name": "Administrator",
"permissions": ["read", "write", "delete"]
},
{
"id": "user",
"name": "User",
"permissions": ["read"]
}
]2. Configuration Data
Set up application configuration:
[
{
"id": "app-config",
"app_name": "My Application",
"version": "1.0.0",
"maintenance_mode": false
}
]3. Test Data
Create sample data for development:
[
{
"id": "customer-001",
"name": "Acme Corporation",
"email": "contact@acme.com",
"status": "active"
},
{
"id": "customer-002",
"name": "Tech Solutions Inc",
"email": "info@techsolutions.com",
"status": "active"
}
]Troubleshooting
Fixtures Not Applied
If fixtures aren't being applied:
- Check file naming:
{doctype_name}.fixture.json - Verify JSON syntax is valid
- Ensure doctype exists and is properly defined
- Run
nailgun migrateornailgun push
Duplicate Records
If you get duplicate record errors:
- Check for existing records with the same IDs
- Use unique IDs in your fixtures
- Consider using
upsertoperations for updates
Invalid Data
If fixtures fail to load:
- Validate JSON syntax
- Check that all required fields are present
- Ensure field types match doctype definitions
- Verify foreign key references exist
Tips
- Keep fixtures small and focused
- Use fixtures for essential system data
- Test fixtures in development before production
- Document the purpose of each fixture file
- Consider using different fixtures for different environments
- Use version control to track fixture changes
Related Commands
nailgun migrate- Apply migrations and fixturesnailgun push- Push changes and apply fixturesnailgun sync- Sync schema without fixturesnailgun scaffold- Create new apps with fixture directories
Create Doctype
Learn how to create new document types (doctypes) in your Zodula project using the scaffold command. Doctypes define the structure and behavior of your data models.
Extend Scripts
Learn how to extend Zodula applications using extend scripts. Use `.on()` to extend doctype events and `bxo` to extend the HTTP server with custom routes and middleware.
Zodula