ramsql icon indicating copy to clipboard operation
ramsql copied to clipboard

Clubbing with gorm throws error on Automigrate

Open abhisekp opened this issue 3 years ago • 1 comments

Getting error while using with gorm.

Error

DB.AutoMigrate(&VSettings{}) throws

2021/05/26 13:35:13 src/ramsql.go:83 Syntax error near BIGINT UNSIGNED not
[0.094ms] [rows:0] CREATE TABLE `vsettings` (`id` BIGINT UNSIGNED NOT NULL,`vid` BIGINT UNSIGNED NOT NULL,`tid` BIGINT UNSIGNED NOT NULL,`settings` JSON NOT NULL,`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,`updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,PRIMARY KEY (`id`))
2021/05/26 13:35:13 Syntax error near BIGINT UNSIGNED not

My implementation for clubbing with gorm

import (
	"database/sql"
	"encoding/json"
	_ "github.com/proullon/ramsql/driver"
	"gorm.io/datatypes"
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"log"
	"time"
)

type ID uint64

// VSettings [...]
type VSettings struct {
	ID        ID             `gorm:"id;type:BIGINT UNSIGNED NOT NULL;primaryKey;autoIncrement;"`
	VID       ID             `gorm:"vid;type:BIGINT UNSIGNED NOT NULL;"`
	TID       ID             `gorm:"tid;type:BIGINT UNSIGNED NOT NULL;"`
	Settings  datatypes.JSON `gorm:"settings;type:JSON;NOT NULL" json:"settings"`
	CreatedAt time.Time      `gorm:"created_at;type:TIMESTAMP;default:CURRENT_TIMESTAMP;"`
	UpdatedAt time.Time      `gorm:"updated_at;type:TIMESTAMP;default:CURRENT_TIMESTAMP;OnUpdate:CURRENT_TIMESTAMP;"`
	//DeletedAt *time.Time     `gorm:"deleted_at;type:TIMESTAMP"`
}

// TableName get sql table name.
func (m *VSettings) TableName() string {
	return "vsettings"
}

func main() {
	sqlDB, err := sql.Open("ramsql", dbDSN)

	if err != nil {
		log.Fatalf("sql.Open : Error : %v\n", err)
	}

	defer func(db *sql.DB) {
		err := db.Close()
		if err != nil {
			log.Fatalf("sqlDB.Close : Error : %v\n", err)
		}
	}(sqlDB)

	DB, err := gorm.Open(
		mysql.New(
			mysql.Config{
				Conn:                      sqlDB,
				SkipInitializeWithVersion: true,
			},
		),
		&gorm.Config{
			DisableForeignKeyConstraintWhenMigrating: true,
		},
	)

	if err != nil {
		log.Fatalf("gorm.Open : Error : %v\n", err)
	}

	err = DB.AutoMigrate(&VSettings{})
	if err != nil {
		log.Println(err)
		log.Fatalf("AutoMigrate VSettings : Error : %v\n", err)
	}
}

abhisekp avatar May 26 '21 08:05 abhisekp

Same for me:

[0.141ms] [rows:0] CREATE TABLE `channels` (`id` bigint unsigned AUTO_INCREMENT,`created_at` datetime(3) NULL,`updated_at` datetime(3) NULL,`deleted_at` datetime(3) NULL,`created` datetime(3) NULL,`channel_identifier` varchar(500),`user_identifier` varchar(500),`time_zone` longtext,`daily_reminder` bigint unsigned,`calendar_secret` varchar(191),`role` longtext,PRIMARY KEY (`id`),INDEX idx_channels_deleted_at (`deleted_at`),INDEX idx_channels_channel_identifier (`channel_identifier`),INDEX idx_channels_user_identifier (`user_identifier`),INDEX idx_channels_calendar_secret (`calendar_secret`))
--- FAIL: TestDatabase_Initialize (0.00s)
    database_test.go:49:
                Error Trace:    database_test.go:49
                Error:          Received unexpected error:
                                Syntax error near bigint unsigned AUTO_INCREMENT
                Test:           TestDatabase_Initialize
                Messages:       Failure on initializing the database: Syntax error near bigint unsigned AUTO_INCREMENT
FAIL
exit status 1
FAIL    github.com/CubicrootXYZ/matrix-reminder-and-calendar-bot/internal/database      0.012s

The auto migrate does not work as the bigint unsigned AUTO_INCREMENT is invalid to ramsql.

CubicrootXYZ avatar Oct 17 '21 15:10 CubicrootXYZ