Skip to content

Feature Request: Model validation #6030

@ExerciseBook

Description

@ExerciseBook

Describe the feature

run the code below

package main

import (
	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
)

type Product struct {
	id    int
	code  string
	price uint
}

func main() {
	db, err := gorm.Open(sqlite.Open("test.db"), &gorm.Config{})
	if err != nil {
		panic("failed to connect database")
	}
	db.AutoMigrate(&Product{})
	var product Product
	db.First(&product, 1)                
}

we got an error

2023/02/06 12:49:57 /内容削除/main.go:21 near "=": syntax error
[0.010ms] [rows:0] SELECT * FROM `products` WHERE `products`. = 1 ORDER BY `products`. LIMIT 1

we can see that GORM generated a sql query without column name because there is no Primary Key Definition in Product struct.

I hope we can get a warning when there is no public field or no primary key.

Motivation

UX

Related Issues

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions