Gorm和许多关系
我有一个问题了解如何使用GORM获取相关信息。这是我第一次主动使用ORM。
我正在尝试获取特定用户ID可以访问的所有设备。有了当前功能,我将获得所有设备,不仅是属于实际用户ID的设备,还包括该设备所属于的用户列表,这是我目前不需要的信息。
有没有一种方法可以获取用户可以使用user_devices使用Gorm访问的设备,或者我应该创建一个自定义查询以实现我想要的东西?
谢谢。
type User struct {
ID uint
Name string
Email string
Age uint8
Birthday time.Time
Password string
ActivatedAt time.Time
OrgID uint
Org Org
Devices []Device `gorm:"many2many:user_devices"`
CreatedAt time.Time
UpdatedAt time.Time
}
type Device struct {
ID uint
Name string
Hwaddr string
OrgID uint
PublicIP uint
Org Org
Users []User `gorm:"many2many:user_devices"`
DeviceType string
Identity string
LastPolledAt time.Time
CreatedAt time.Time
UpdatedAt time.Time
}
func (u *Device) FindAllDevicesByUid(db *gorm.DB, user *User) (*[]Device, error) {
var err error
devices := []Device{}
err = db.Debug().Preload("Users", "user_id = ?", user.ID).Find(&devices).Error
if err != nil {
return &[]Device{}, err
}
return &devices, err
}
结果:
{
"ID": 4,
"Name": "Test 4",
"Hwaddr": "00:00:00:00:00:04",
"OrgID": 1,
"PublicIP": 0,
"Org": {
"Id": 0,
"Name": "",
"Users": null,
"CreatedAt": "0001-01-01T00:00:00Z",
"UpdatedAt": "0001-01-01T00:00:00Z"
},
"Users": [
{
"ID": 1,
"Name": "Jan Astrup",
"Email": "[email protected]",
"Age": 0,
"Birthday": "0001-01-01T00:00:00Z",
"ActivatedAt": "2022-04-24T15:48:40+02:00",
"OrgID": 1,
"Org": {
"Id": 0,
"Name": "",
"Users": null,
"CreatedAt": "0001-01-01T00:00:00Z",
"UpdatedAt": "0001-01-01T00:00:00Z"
},
"Devices": null,
"CreatedAt": "2022-04-24T15:48:40+02:00",
"UpdatedAt": "2022-04-24T15:48:40+02:00"
}
],
"DeviceType": "Router",
"Identity": "M1923 4",
"LastPolledAt": "0001-01-01T00:00:00Z",
"CreatedAt": "2022-04-24T15:17:30+02:00",
"UpdatedAt": "2022-04-24T15:17:30+02:00"
}
I have an issue understanding how to fetch related information using gorm. This is the first time ever I'm using an ORM actively.
I'm trying to get all devices that a specific user id has access to. With the current function I get all devices, not only the device belonging to the actual user id, and it also includes the list of users that the device belongs to, which is information i really don't need at this point.
Is there a way to Get the devices that the user has access to using the user_devices join table using gorm, or is this something i should create a custom query in order to achieve what I want?
Thank you.
type User struct {
ID uint
Name string
Email string
Age uint8
Birthday time.Time
Password string
ActivatedAt time.Time
OrgID uint
Org Org
Devices []Device `gorm:"many2many:user_devices"`
CreatedAt time.Time
UpdatedAt time.Time
}
type Device struct {
ID uint
Name string
Hwaddr string
OrgID uint
PublicIP uint
Org Org
Users []User `gorm:"many2many:user_devices"`
DeviceType string
Identity string
LastPolledAt time.Time
CreatedAt time.Time
UpdatedAt time.Time
}
func (u *Device) FindAllDevicesByUid(db *gorm.DB, user *User) (*[]Device, error) {
var err error
devices := []Device{}
err = db.Debug().Preload("Users", "user_id = ?", user.ID).Find(&devices).Error
if err != nil {
return &[]Device{}, err
}
return &devices, err
}
Result:
{
"ID": 4,
"Name": "Test 4",
"Hwaddr": "00:00:00:00:00:04",
"OrgID": 1,
"PublicIP": 0,
"Org": {
"Id": 0,
"Name": "",
"Users": null,
"CreatedAt": "0001-01-01T00:00:00Z",
"UpdatedAt": "0001-01-01T00:00:00Z"
},
"Users": [
{
"ID": 1,
"Name": "Jan Astrup",
"Email": "[email protected]",
"Age": 0,
"Birthday": "0001-01-01T00:00:00Z",
"ActivatedAt": "2022-04-24T15:48:40+02:00",
"OrgID": 1,
"Org": {
"Id": 0,
"Name": "",
"Users": null,
"CreatedAt": "0001-01-01T00:00:00Z",
"UpdatedAt": "0001-01-01T00:00:00Z"
},
"Devices": null,
"CreatedAt": "2022-04-24T15:48:40+02:00",
"UpdatedAt": "2022-04-24T15:48:40+02:00"
}
],
"DeviceType": "Router",
"Identity": "M1923 4",
"LastPolledAt": "0001-01-01T00:00:00Z",
"CreatedAt": "2022-04-24T15:17:30+02:00",
"UpdatedAt": "2022-04-24T15:17:30+02:00"
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要仅加载特定用户的设备,您可以使用
加入
函数连接表user_devices
和device> device
。除此之外,您始终可以加载用户并预加载其设备:
然后,您可以使用
U.Devices
返回用户的设备,但是我认为此方法执行两个查询以获取用户的设备。To load just the devices for a specific user, you can use the
Joins
function to join tablesuser_devices
anddevices
.Apart from this, you can always load the user and preload its devices:
Then, you can use
u.Devices
to return the user's devices, but I think this approach executes two queries to get the user's devices.