SQL to Go Struct
| SQL type | Go type |
|---|---|
| INT, INTEGER, SMALLINT, TINYINT | int, int32 or int64, depending on the range and application |
| BIGINT | int64 |
| FLOAT, REAL | float32 |
| DOUBLE, DOUBLE PRECISION | float64 |
| DECIMAL(p,s), NUMERIC(p,s) | For exact decimals, use a decimal type implementing sql.Scanner and driver.Valuer. Floating-point types can lose precision. |
| CHAR(n), VARCHAR(n), TEXT | string or sql.NullString for nullable values |
| DATE | time.Time |
| DATETIME, TIMESTAMP | time.Time |
| BOOLEAN | bool or sql.NullBool for nullable values |
Additional packages
Import gorm.io/datatypes if the generated code uses its types.
Try this example
CREATE TABLE `foo` (
`id` BIGINT unsigned NOT NULL AUTO_INCREMENT COMMENT 'pk',
`code` VARCHAR(191) NOT NULL COMMENT 'code',
`properties` JSON COMMENT 'properties',
`is_deleted` TINYINT unsigned NOT NULL DEFAULT 0 COMMENT '0 valid/1 deleted',
`create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'create_time',
`update_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'update_time',
INDEX `idx_create_time` (`create_time`),
UNIQUE `uniq_code` (`code`),
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARACTER SET utf8mb4 COMMENT='comments';
Feedback