Skip to content

nil pointer error when i using gen generating code #1352

@jingchenLu

Description

@jingchenLu

Description

When using the sqlite database, if a certain field has a default value, the gen model generation error occurs:

panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0xa8 pc=0xb1d905]

goroutine 1 [running]:
gorm.io/gen/internal/model.(*Column).needDefaultTag(0xc00032cc80, {0xc0002e37f5, 0x4})
        D:/golang/GoWorks/pkg/mod/gorm.io/[email protected]/internal/model/tbl_column.go:131 +0x45
gorm.io/gen/internal/model.(*Column).buildGormTag(0xc00032cc80)
        D:/golang/GoWorks/pkg/mod/gorm.io/[email protected]/internal/model/tbl_column.go:114 +0x985
gorm.io/gen/internal/model.(*Column).ToField(0xc00032cc80, 0x1, 0x0, 0x0)
        D:/golang/GoWorks/pkg/mod/gorm.io/[email protected]/internal/model/tbl_column.go:73 +0x2d8
gorm.io/gen/internal/generate.getFields(0xc0002c5860, 0xc000346000, {0xc000342540, 0x7, 0x5?})
        D:/golang/GoWorks/pkg/mod/gorm.io/[email protected]/internal/generate/generate.go:24 +0x15a
gorm.io/gen/internal/generate.GetQueryStructMeta(0xc0002c5860, 0xc00029be70?)
        D:/golang/GoWorks/pkg/mod/gorm.io/[email protected]/internal/generate/export.go:51 +0x335
gorm.io/gen.(*Generator).GenerateModelAs(0xc0002da200, {0xc00029be70, 0x5}, {0xc00032ab48?, 0x153e700?}, {0x0?, 0xc00006af20?, 0xc000120580?})
        D:/golang/GoWorks/pkg/mod/gorm.io/[email protected]/generator.go:127 +0x5d
gorm.io/gen.(*Generator).GenerateModel(0xc0002da200, {0xc00029be70, 0x5}, {0x0, 0x0, 0x0})
        D:/golang/GoWorks/pkg/mod/gorm.io/[email protected]/generator.go:122 +0x6b
main.main()
exit status 2

My code:

appConfig := config.GetConfig()

db := migrations.InitDB(appConfig.DBPath)
log.Println("数据库初始化完成")

g := gen.NewGenerator(gen.Config{
    OutPath: "./dao/query",
    Mode:    gen.WithDefaultQuery | gen.WithQueryInterface,
    // 表字段可为null时,对应字段使用指针类型
    FieldNullable: true,
})

g.UseDB(db)

g.GenerateModel("users")

// 执行代码生成
g.Execute()

sql

CREATE TABLE "users" (
  "id" integer PRIMARY KEY AUTOINCREMENT,
  "username" text NOT NULL,
  "password_hash" text NOT NULL,
  "role" text DEFAULT 'user',
  "created_at" datetime,
  "updated_at" datetime,
  "deleted_at" datetime,
  CONSTRAINT "uni_users_username" UNIQUE ("username" ASC)
);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions