Please answer these questions before submitting your issue. Thanks!
- What version of Go and beego are you using (
bee version
)?
Go version: 1.16.4
bee version
| ___
| |/ / ___ ___
| ___ \ / _ \ / _
| |/ /| /| /
__/ _| ___| v2.0.2
├── GoVersion : go1.16.4
├── GOOS : linux
├── GOARCH : amd64
├── NumCPU : 4
├── GOPATH : /root/goworkspace/packages
├── GOROOT : /usr/local/go
├── Compiler : gc
└── Published : 2020-12-16
- What operating system and processor architecture are you using (
go env
)?
os : linux ,
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
- What did you do?
Hi Team.
I am using ENTERPRISE model and ROLES model using two Many-To-Many relationships (dependent table:TestEnterprisesTestRoless )
package models
import (
"github.com/astaxie/beego/orm"
)
//enterprise ... parent
type ENTERPRISE struct {
ID int64 `orm:"pk;auto;column(id)" json:"id"`
FIRST_NAME string `validate:"required" json:"first_name" orm:"column(first_name);size(100);default(null)"`
EMAIL string `validate:"required" json:"email" orm:"unique;column(email);size(100);default(null)"`
LAST_NAME string `validate:"required" json:"last_name" orm:"column(last_name);default(null)"`
CREATED_AT time.Time `orm:"auto_now;type(datetime);column(createdAt);default(null)"`
UPDATED_AT time.Time `orm:"auto_now_add;type(datetime);column(updatedAt);default(null)"`
ROLES []*ROLES `json:"roles" orm:"rel(m2m);rel_through(ftp-api/services.TestEnterprisesTestRoless);column(role_id);default(null)"`
}
type ROLES struct {
ID int64 `orm:"pk;auto;column(id)" json:"id"`
NAME string `validate:"required" json:"name" orm:"unique;column(name);size(100);default(null)"`
ACTION string `validate:"required" json:"action" orm:"unique;column(action);size(100);default(null)"`
ENTERPRISE []*ENTERPRISE `validate:"required" json:"enterprise" orm:"reverse(many);column(enterprise_id);rel_through(ftp-api/services.TestEnterprisesTestRoless);default(null)"`
}
type TestEnterprisesTestRoless struct {
ID int64 `orm:"pk;auto;column(id)" json:"id"`
ENTERPRISE *ENTERPRISE `orm:"rel(fk);column(enterprise_id)"`
ROLES *ROLES `orm:"rel(fk);column(role_id)"`
}
var OrmConnection orm.Ormer
func (enterpriseObj *ENTERPRISE) TableName() string {
return "test_enterprises"
}
func (addrObj *TestEnterprisesTestRoless) TableName() string {
return "test_enterprises_test_roless"
}
func (roleObj *ROLES) TableName() string {
return "test_roles"
}
//loading the schemas
orm.RegisterModel(new(ROLES), new(ENTERPRISE), new(TestEnterprisesTestRoless))
I am able to create enterprise and roles individually (but the enterprise_id and role_id is not storing in the test_enterprises_test_roless table),
my question is :
how to fetch enterprises with dependent roles and same as
how to fetch roles with dependent enterprises
I have tried using below queries
1 :
//FetchRoleData ... fetching headers and dependent enterprises
func FetchRoleData() ([]ROLES, error) {
roleObj := make([]ROLES, 0)
tableName := new(ROLES).TableName()
querySet := OrmConnection.QueryTable(tableName)
//if any filter/limit required need to be applied to wuery set
num, err := querySet.RelatedSel().All(&roleObj)
if err != nil {
return roleObj, err
}
for _, role := range roleObj {
OrmConnection.LoadRelated(&role, "TestEnterprisesTestRoless", "role_id")
}
log.Printf("selectd %d rows", num)
return roleObj, nil
}
for the above getting error as
" name &{%!s(int64=1) enterprise5 download []}
for model TestEnterprisesTestRoless
is not an available rel/reverse field"
func FetchRoleData() ([]ROLES, error) {
roleObj := make([]ROLES, 0)
tableName := new(ROLES).TableName()
querySet := OrmConnection.QueryTable(tableName)
//if any filter/limit required need to be applied to wuery set
num, err := querySet.RelatedSel("TestEnterprisesTestRoless").All(&roleObj)
if err != nil {
return roleObj, err
}
log.Printf("selectd %d rows", num)
return roleObj, nil
}
for the above getting error as
"unknown model/table name TestEnterprisesTestRoless
"
- What did you expect to see?
sample expected result :
-
role {
"name":"sample rolename",
"action":"update",
"enterprises":[
{
// enterprise attached to this role
},
{
// enterprise attached to this role
}
]
}
-
Enterprise {
"firstName":"",
"lastName":"",
"roles":[
{
// role attached to this enterprise
},
{
// role attached to this enterprise
}
]
}
-
What did you see instead?
name &{%!s(int64=1) enterprise5 download []}
for model TestEnterprisesTestRoless
is not an available rel/reverse field"
unknown model/table name `TestEnterprisesTestRoless
do i need to change anything in the models or do i need to change query in different way
any response will be helpful and appreciated
kind/bug