Skip to content

nil pointer error when i using gen generating code #1285

Open
@novice2194

Description

@novice2194

Description

Throwing an error while generating code using the official Gen example.
The error stack is as follows:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0xa8 pc=0x2390645]

goroutine 1 [running]:
gorm.io/gen/internal/model.(*Column).needDefaultTag(0xc00056c280, {0xc0002a74bd, 0x10})
        /Users/.../go/pkg/mod/gorm.io/gen@v0.3.26/internal/model/tbl_column.go:131 +0x45
gorm.io/gen/internal/model.(*Column).buildGormTag(0xc00056c280)
        /Users/.../go/pkg/mod/gorm.io/gen@v0.3.26/internal/model/tbl_column.go:114 +0x985
gorm.io/gen/internal/model.(*Column).ToField(0xc00056c280, 0x0, 0x0, 0x0)
        /Users/.../go/pkg/mod/gorm.io/gen@v0.3.26/internal/model/tbl_column.go:73 +0x2d8
gorm.io/gen/internal/generate.getFields(0xc00043c240, 0xc0002ab560, {0xc00011cbc0, 0x4, 0x10?})
        /Users/.../go/pkg/mod/gorm.io/gen@v0.3.26/internal/generate/generate.go:24 +0x15a
gorm.io/gen/internal/generate.GetQueryStructMeta(0xc00043c240, 0xc000540660?)
        /Users/.../go/pkg/mod/gorm.io/gen@v0.3.26/internal/generate/export.go:51 +0x335
gorm.io/gen.(*Generator).GenerateModelAs(0xc0001990e0, {0xc000540660, 0x10}, {0xc000541750?, 0x1c9f849?}, {0x0?, 0x276e9c0?, 0xc000021301?})
        /Users/.../go/pkg/mod/gorm.io/gen@v0.3.26/generator.go:115 +0x5d
gorm.io/gen.(*Generator).GenerateModel(0xc0001990e0, {0xc000540660, 0x10}, {0x0, 0x0, 0x0})
        /Users/.../go/pkg/mod/gorm.io/gen@v0.3.26/generator.go:110 +0x6b
gorm.io/gen.(*Generator).GenerateAllTable(0xc0001990e0, {0x0, 0x0, 0x0})
        /Users/.../go/pkg/mod/gorm.io/gen@v0.3.26/generator.go:141 +0x17c
main.main()
        /Users/.../demo/cmd/gen.go:38 +0x17d
exit status 2

Code like it:

func main() {
	g := gen.NewGenerator(gen.Config{
		OutPath: "../query",
		Mode:    gen.WithoutContext | gen.WithDefaultQuery | gen.WithQueryInterface, // generate mode
	})

	gormdb, _ := gorm.Open(sqlite.Open(globals.RootPath.AppData))
	g.UseDB(gormdb) // reuse your gorm db

	// Generate basic type-safe DAO API for struct `model.User` following conventions

	g.ApplyBasic(
		// Generate struct `User` based on table `users`
		g.GenerateModel("users"),

		// // Generate struct `Employee` based on table `users`
		// g.GenerateModelAs("users", "Employee"),
		//
		// // Generate struct `User` based on table `users` and generating options
		// g.GenerateModel("users", gen.FieldIgnore("address"), gen.FieldType("id", "int64")),
		//
		// // Generate struct `Customer` based on table `customer` and generating options
		// // customer table may have a tags column, it can be JSON type, gorm/gen tool can generate for your JSON data type
		// g.GenerateModel("customer", gen.FieldType("tags", "datatypes.JSON")),
	)
	g.ApplyBasic(
		// Generate structs from all tables of current database
		g.GenerateAllTable()...,
	)
	// Generate the code
	g.Execute()
}

database sql:

CREATE TABLE IF NOT EXISTS users (
	id SERIAL PRIMARY KEY,          
	tenant_id INT NOT NULL,        
	username VARCHAR(255) NOT NULL, 
	status INT NOT NULL,            
	description TEXT,
	created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,  
	updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,  
	deleted_at TIMESTAMP  
);

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