FAQ
General Questions
What databases does GoCRUD support?
GoCRUD supports:
- PostgreSQL
- MySQL
- SQLite
- Microsoft SQL Server
Does GoCRUD support PATCH operations?
Yes, through Huma's autopatch feature. Enable it with:
Can I use custom field types?
Yes, by implementing the Operations
method for custom filtering:
type CustomID int
func (_ *CustomID) Operations() map[string]func(string, ...string) string {
return map[string]func(string, ...string) string{
"_regexp": func(key string, values ...string) string {
return fmt.Sprintf("%s REGEXP %s", key, values[0])
},
}
}
Common Issues
Why am I getting "unsupported database driver"?
Make sure you:
- Import the correct database driver
- Use the supported driver package:
- PostgreSQL:
github.com/lib/pq
- MySQL:
github.com/go-sql-driver/mysql
- SQLite:
github.com/mattn/go-sqlite3
- MSSQL:
github.com/microsoft/go-mssqldb
- PostgreSQL:
Why aren't my relations working?
Check that you:
- Used the correct tag format:
- Set up the foreign key fields correctly
- Have proper database permissions
Why isn't pagination working?
Ensure you're using both limit
and skip
:
Best Practices
How should I structure my models?
Follow these guidelines:
- Always define table name using the
_
field: - Make ID fields pointers for proper null handling:
- Use appropriate tags for validation:
How can I optimize performance?
- Use bulk operations when possible
- Set appropriate limits on queries
- Add database indexes for filtered fields
- Use relationship filtering judiciously
How should I handle errors?
- Use hooks for validation:
- Return specific error types
- Log errors appropriately
Configuration
How do I disable certain operations?
Use operation modes in config:
config := &gocrud.Config[User]{
GetMode: gocrud.BulkSingle,
PostMode: gocrud.None, // Disable POST
DeleteMode: gocrud.None, // Disable DELETE
}
How do I add custom validation?
Use before hooks:
BeforePost: func(ctx context.Context, models *[]User) error {
for _, user := range *models {
if err := customValidation(user); err != nil {
return err
}
}
return nil
}
Advanced Usage
Can I use transactions?
Transactions are handled automatically for:
- Bulk updates
- Bulk deletes
- Operations with hooks
How do I implement custom filtering?
- Define custom operations on field types
- Use the operations in queries:
Can I extend the default operations?
Yes, by:
- Implementing custom field types
- Adding hooks for custom logic
- Using the underlying repository interface
Troubleshooting
Common Error Messages
"entity not found"
- Check if the resource exists
- Verify the ID is correct
- Ensure proper database permissions
"invalid identifier type"
- Check model field types
- Ensure ID fields are properly defined
- Verify query parameter types
"validation failed"
- Check input data format
- Verify field constraints
- Look for missing required fields
Debug Tips
- Enable debug logging
- Check SQL queries in logs
- Verify database connection
- Test queries directly in database
- Check hook execution order
Getting Help
Where can I find more examples?
Check the examples directory in the repository:
- Basic CRUD
- Relations
- Custom operations
- Different databases
- Hooks implementation
How do I report issues?
- Check existing issues on GitHub
- Provide minimal reproduction
- Include:
- Go version
- Database type and version
- Complete error message
- Example code