GORM Playground Link

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

Description

table t_user

CREATE TABLE `t_user` (
  `id` int unsigned NOT NULL AUTO_INCREMENT COMMENT 'user ID',
  `name` varchar(255) DEFAULT NULL COMMENT 'user name',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COMMENT='user table';

table t_device

CREATE TABLE `t_device` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8mb3_bin DEFAULT NULL,
  `created_by` int NOT NULL,
  `created_at` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3 COLLATE=utf8mb3_bin COMMENT='device table';

User struct

type User struct {
    ID       int64   `gorm:"type:int(11);autoIncrement;primaryKey;column:id" json:"id"`
    Name     string  `gorm:"type:string;size:100" json:"name"`
}

Device struct 1

type Device struct {
    ID            int64      `gorm:"type:int(11);autoIncrement;primaryKey;column:id" json:"id"`
    Name          string     `gorm:"type:string;size:255" json:"name"`
    CreatedBy     int64      `gorm:"type:int(11)" json:"createdBy"`
    CreatedByUser *User      `gorm:"foreignKey:CreatedBy" json:"createdByUser"`
    CreatedAt     time.Time  `json:"createdAt"`
}

Device struct 2

type Device struct {
    ID            int64      `gorm:"type:int(11);autoIncrement;primaryKey;column:id" json:"id"`
    Name          string     `gorm:"type:string;size:255" json:"name"`
    CreatedID     int64      `gorm:"type:int(11)" json:"createdBy"`
    CreatedByUser *User      `gorm:"foreignKey:CreatedID" json:"createdByUser"`
    CreatedAt     time.Time  `json:"createdAt"`
}

query

var devices = make([]*models.Device, 0)
db.Model(new(models.Device)).Preload("CreatedByUser").Find(&devices).Error

query with device struct 1, cannot preload CreatedByUser normally. query with device struct 2, works ok.

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