The code generator creates model and repository boilerplate from SQL files.
Entry point:
command/codegen/handler.go
Default SQL input directory:
bin/data/sql
Default output directories:
- model:
app/model - repository:
app/repository
- Go
1.24.x - SQL schema files under
bin/data/sql
Generate one table:
go run ./command/codegen/handler.go -name auth_appGenerate all SQL files:
go run ./command/codegen/handler.goGenerate one PostgreSQL SQL file:
go run ./command/codegen/handler.go -dialect=postgres -sql bin/data/sql/postgres -name oauth_appGenerate with custom paths:
go run ./command/codegen/handler.go -sql custom/sql -model custom/model -repo custom/repo| Option | Description |
|---|---|
-name |
SQL file name without .sql |
-sql |
SQL input directory |
-model |
Model output directory |
-repo |
Repository output directory |
-service |
Service output directory (if supported by generator version) |
-force |
Overwrite existing files |
- Use standard MySQL
CREATE TABLEstatements. - Keep table/column comments if you want meaningful generated field comments.
- Keep SQL file name aligned with generated module name.
Example:
CREATE TABLE `users` (
`id` int NOT NULL AUTO_INCREMENT COMMENT 'Primary key',
`user_name` varchar(64) NOT NULL COMMENT 'User name',
`email` varchar(128) DEFAULT NULL COMMENT 'Email',
`status` tinyint(1) NOT NULL DEFAULT '1' COMMENT 'Status',
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='Users';Typical generated outputs:
- model file in
app/model/<module>/ - repository file in
app/repository/<module>/
Generated code usually includes:
- struct fields with tags
- basic CRUD helpers
- repository interface and implementation skeleton
After generation, you still need to:
- Wire service logic (
app/service/...) - Add controller handlers (
app/http/controller/...) - Register routes (
app/http/router/...) - Add/update tests
- Update docs and error-code/i18n mapping if behavior is user-facing
In repository code, prefer model-layer helper methods first.
Direct db.WithContext(ctx)... should be limited to:
- transactions
- query shapes not covered by model helpers (
Select,Pluck,Joins)
Run at least:
gofmt -w .
go test ./...Recommended for medium/large changes:
go test -race ./...