go-gorm / gorm

The fantastic ORM library for Golang, aims to be developer friendly
https://gorm.io
MIT License
36.19k stars 3.88k forks source link

`ERRO[0000] near "(": syntax error ` in automigraion for sqlite db. #7037

Open puni9869 opened 2 months ago

puni9869 commented 2 months ago

Your Question

I have a user model for gorm that creates a query.

CREATE TABLE `users` (`id` uuid DEFAULT gen_random_uuid(),`first_name` text,`last_name` text,`display_name` text,`password` text,`email_verify_hash` text,`is_email_verified` numeric,`is_active` numeric,`is_profile_public` numeric,`email` text NOT NULL,`activated_at` datetime,`created_at` datetime,`updated_at` datetime,`alternate_email` text,PRIMARY KEY (`id`),CONSTRAINT `uni_users_id` UNIQUE (`id`),CONSTRAINT `uni_users_email` UNIQUE (`email`))

Gorm Model:-

type User struct {
    ID              uuid.UUID `gorm:"primaryKey;unique;type:uuid;default:gen_random_uuid()"` // Standard field for the primary key
    FirstName       string   
    LastName        string    
    DisplayName     string    
    Password        string    
    EmailVerifyHash string   
    IsEmailVerified bool      
    IsActive        bool      
    IsProfilePublic bool    
    Email           string    `gorm:"unique;not null"` 
    ActivatedAt     time.Time 
    CreatedAt       time.Time 
    UpdatedAt       time.Time 
    AlternateEmail  string    
}

Its giving an error ERRO[0000] near "(": syntax error in sqlite db while automigrate the models.

image

The document you expected this should be explained

This model is automigrated to postgres gorm. But there is something off with sqlite part of gorm. I am using following go dependencies

gorm.io/driver/postgres v1.5.7
gorm.io/driver/sqlite v1.5.5
gorm.io/gorm v1.25.10

Expected answer

Please help me in this issue. Am I missing something in my implementation. Thanks in advance.

puni9869 commented 1 month ago

Can anyone help me in this...

mkoziy commented 1 week ago

Try the following in your model. Worked for me.

ID              uuid.UUID `gorm:"primaryKey;unique;type:uuid;default:(generate_random_uuid())"`