GORM Playground Link

https://github.com/go-gorm/playground/pull/1

// main.go
package main

import (
    "database/sql"
    "time"

    "gorm.io/driver/mysql"
    "gorm.io/gorm"
)

// User 有一张 CreditCard,UserID 是外键
type User struct { // 拥有者
    gorm.Model
    Name         string `gorm:"default:haotian"`
    Email        *string
    Age          uint8 `gorm:"default:30"`
    Birthday     *time.Time
    MemberNumber sql.NullString
    ActivatedAt  sql.NullTime
    Active       sql.NullBool `gorm:"default:true"`
    CreatedAt    time.Time
    UpdatedAt    time.Time
    CreditCard   CreditCard // 一对一的关系

    FirstName string
    LastName  string

    FullName string `gorm:"->;type:GENERATED ALWAYS AS (concat(firstname,' ',lastname));default:(-);"`

    // ALTER TABLE `users` add  COLUMN `full_name` longtext  generated always as (concat(users.first_name,' ',users.last_name));
}


func main() {
    // 参考 https://github.com/go-sql-driver/mysql#dsn-data-source-name 获取详情
    dsn := "root:123456@tcp(localhost:3306)/gorm_demo?charset=utf8mb4&parseTime=True&loc=Local"
    db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})

    if err != nil {
        panic("failed to connect database")
    }

    db.Debug().AutoMigrate(&User{})
    db.Debug().AutoMigrate(&CreditCard{})
}

Description

code is from this artical

Gorm generated always as run SQL syntax Error

It's Error

Gorm generated always as run SQL syntax Error

It's ok Gorm generated always as run SQL syntax Error

missing data_type (longtext)

#  SQL syntax
 col_name data_type [GENERATED ALWAYS] AS (expression)
   [VIRTUAL | STORED] [NOT NULL | NULL]
   [UNIQUE [KEY]] [[PRIMARY] KEY]
   [COMMENT 'string']

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: github-actions[bot]

The issue has been automatically marked as stale as it missing playground pull request link, which is important to help others understand your issue effectively and make sure the issue hasn't been fixed on latest master, checkout https://github.com/go-gorm/playground for details. it will be closed in 30 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking

Comment From: OrkhanAlikhanov

Any workarounds for this issue?

Comment From: meshwara

On postgresql, I have to set the column like this. I think column type and STORED is madatory in postgresql.

type TaskFinish struct {
    DueDate   types.Date `json:"due_date" gorm:"not null"`
    EndDate types.Date `json:"end_date" gorm:"not null;default:NOW()"`
    Delta int `json:"delta" gorm:"->;not null;type:integer GENERATED ALWAYS AS (due_date - end_date) STORED;"`
}