How to set the table relationship of beego

user User
user order Order

want to implement
A user can have N orders, and each order can only have one user
< hr >

Model

type User struct {
    Id    int64
    Name    string
    Order    []*Order    `orm:"reverse(many)"`
}

type Order struct {
    Id    int64
    User    *User    `orm:"rel(fk)"`
    CreatedAt    time.Time
}

data Table:
user

< table > < thead > < tr > < th > id < / th > < th > name < / th > < / tr > < / thead > < tbody > < tr > < td > 1 < / td > < td > test < / td > < / tr > < tr > < td > 2 < / td > < td > testbbbb < / td > < / tr > < / tbody > < / table >

order

< table > < thead > < tr > < th > id < / th > < th > user_id (foreign key) < / th > < th > created_at < / th > < / tr > < / thead > < tbody > < tr > < td > 1 < / td > < td > 1 < / td > < td > 2018-12-20 10:36:47 < / td > < / tr > < tr > < td > 2 < / td > < td > 1 < / td > < td > 2018-12-20 10:38:09 < / td > < / tr > < tr > < td > 3 < / td > < td > 1 < / td > < td > 2018-12-20 10:40:12 < / td > < / tr > < tr > < td > 4 < / td > < td > 2 < / td > < td > 2018-12-20 10:47:52 < / td > < / tr > < / tbody > < / table >

get order data as set above
:

{
    "Id":1,
    "CreatedAt":"2018-12-20T10:36:47+08:00",
    "User":{
        "Id":1,
        "Name":"test",
        "Order":null
    }
}

gets user data, however, the value that gets Order is null . If you want to get all the orders obtained by a user, do you want to process them through business logic?

{
    "Id":1,
    "Name":"test",
    "Order":null
}

the initial contact with beego, documents made me look confused! Ask for advice!

Mar.03,2022

type User struct {
    Id    int64
    Name    string
}

type Order struct {
    Id    int64
    User    *User `orm:"rel(fk)"`
    CreatedAt    time.Time
}

add user conditions to the query

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e9f405-464c1.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e9f405-464c1.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?