go-admin icon indicating copy to clipboard operation
go-admin copied to clipboard

[Question]数据表格和另一个表进行关联后显示异常的问题

Open moon-hzq opened this issue 5 years ago • 10 comments

代码版本:1.2.16

问题描述 [详细地描述问题,让大家都能理解]

概述:在一个数据表格中,其中一个字段是从另一张表关联后获取展示的,在列表页能正常显示,但进入编辑页和详情页后,该字段没有任何显示输出 举例:一张文章表(articles),是数据表格的主表,另一张是文章内容表(articles_content),两张表关联后,显示articles_content表中的 content 字段,在列表页能正常显示,但点击该文章的编辑和详情页时,content字段的内容是空白

示例代码 [如果有必要,展示代码,线上示例,或仓库]

info := articles.GetInfo() .... info.AddField("Content", "content", db.Text ). FieldJoin(types.Join{ Table: "articles_content", Field: "id", JoinField: "article_id", }) .... formList := articles.GetForm() .... formList.AddField("Content","content", db.Text, form.Text) .... detail := articles.GetDetail() .... detail.AddField("Content", "content", db.Text ). FieldJoin(types.Join{ Table: "articles_content", Field: "id", JoinField: "article_id", }) ...

其他信息 [如截图等其他信息可以贴在这里]

是以上使用方法不对吗

moon-hzq avatar Oct 29 '20 09:10 moon-hzq

贴下两张表的结构

Lifelong-Study avatar Oct 30 '20 13:10 Lifelong-Study

贴下两张表的结构

CREATE TABLE articles ( id int(11) NOT NULL AUTO_INCREMENT COMMENT '资讯ID', author_id int(11) NOT NULL DEFAULT '0' COMMENT '作者ID', author_name varchar(50) NOT NULL DEFAULT '' COMMENT '作者', short_title varchar(100) DEFAULT '' COMMENT '短标题', title varchar(200) NOT NULL DEFAULT '' COMMENT '标题', source varchar(200) NOT NULL DEFAULT '' COMMENT '来源', pic_url text COMMENT '图片地址', pic_type tinyint(4) DEFAULT '0' COMMENT '6种类型:,0:无图,1:一张小图,2:一张大图,3:三张小图,4:小图视频,5:大图视频', status tinyint(4) DEFAULT '0' COMMENT '状态:0:草稿,1.待审核 2.审核通过,3.未通过,4.撤回,5.删除', is_top tinyint(4) DEFAULT '0' COMMENT '是否置顶:0:未置顶,1.置顶', sort_index int(4) DEFAULT '0' COMMENT '排序倒叙', create_time int(11) DEFAULT '0' COMMENT '创建时间', update_time int(11) DEFAULT '0' COMMENT '修改时间', top_time int(11) DEFAULT '0' COMMENT '置顶时间', deleted int(1) DEFAULT '0' COMMENT '删除:0:未删除,1:删除', deleted_time bigint(11) DEFAULT '0' COMMENT '删除时间', PRIMARY KEY (id) USING BTREE, KEY idx_status (status,author_id) USING BTREE, KEY idx_is_top (status,is_top) USING BTREE, KEY idx_create_time (status,create_time) USING BTREE ) ENGINE=InnoDB AUTO_INCREMENT=32 DEFAULT CHARSET=utf8mb4 COMMENT='资讯表';

CREATE TABLE articles_content ( article_id int(11) NOT NULL DEFAULT '0' COMMENT '资讯ID', content text COMMENT '内容', PRIMARY KEY (article_id) USING BTREE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='资讯内容表';

moon-hzq avatar Nov 02 '20 02:11 moon-hzq

//        viewer: A 表
// customer_name: B 表中要展示的栏位名称
viewer.AddField(lang.Username(), "customer_name", db.Bigint).
	FieldJoin(types.Join{
		Table:     "ez_customer",	// B 表名称
		JoinField: "id",		// B 表栏位
		Field:     "uid",		// A 表栏位
	})

Hope this can help you

Lifelong-Study avatar Nov 03 '20 14:11 Lifelong-Study

我的问题里面就是这样做的,但是显示不出来,,打印sql语句是这样的: {"statement": "select articles.author_id,articles.author_name,articles.short_title,articles.title,articles.source,articles.pic_url,articles.pic_type,articles.status,articles.is_top,articles.sort_index,articles.id,group_concat(articles_content.content separator 'Z5dDrivernSKR') as articles_content_goadmin_join_content from articles left join articles_content on articles_content.article_id = articles.id GROUP BY articles.id order by articles.id desc LIMIT ? OFFSET ?", "args": [10,0]}

发现其中有这句话: group_concat(articles_content.content separator 'Z5dDrivernSKR') as articles_content_goadmin_join_content

//        viewer: A 表
// customer_name: B 表中要展示的栏位名称
viewer.AddField(lang.Username(), "customer_name", db.Bigint).
	FieldJoin(types.Join{
		Table:     "ez_customer",	// B 表名称
		JoinField: "id",		// B 表栏位
		Field:     "uid",		// A 表栏位
	})

Hope this can help you

我的问题里面就是这样做的,但是显示不出来: info.AddField("Content", "content", db.Text ). FieldJoin(types.Join{ Table: "articles_content", Field: "id", JoinField: "article_id", })

打印sql语句是这样的: {"statement": "select articles.author_id,articles.author_name,articles.short_title,articles.title,articles.source,articles.pic_url,articles.pic_type,articles.status,articles.is_top,articles.sort_index,articles.id,group_concat(articles_content.content separator 'Z5dDrivernSKR') as articles_content_goadmin_join_content from articles left join articles_content on articles_content.article_id = articles.id GROUP BY articles.id order by articles.id desc LIMIT ? OFFSET ?", "args": [10,0]}

发现其中有这句话: group_concat(articles_content.content separator 'Z5dDrivernSKR') as articles_content_goadmin_join_content 这个 和 实际的 content 字段是不匹配的

moon-hzq avatar Nov 04 '20 09:11 moon-hzq

我的问题里面就是这样做的,但是显示不出来,,打印sql语句是这样的: {"statement": "select articles.author_id,articles.author_name,articles.short_title,articles.title,articles.source,articles.pic_url,articles.pic_type,articles.status,articles.is_top,articles.sort_index,articles.id,group_concat(articles_content.content separator 'Z5dDrivernSKR') as articles_content_goadmin_join_content from articles left join articles_content on articles_content.article_id = articles.id GROUP BY articles.id order by articles.id desc LIMIT ? OFFSET ?", "args": [10,0]}

发现其中有这句话: group_concat(articles_content.content separator 'Z5dDrivernSKR') as articles_content_goadmin_join_content

//        viewer: A 表
// customer_name: B 表中要展示的栏位名称
viewer.AddField(lang.Username(), "customer_name", db.Bigint).
	FieldJoin(types.Join{
		Table:     "ez_customer",	// B 表名称
		JoinField: "id",		// B 表栏位
		Field:     "uid",		// A 表栏位
	})

Hope this can help you

我的问题里面就是这样做的,但是显示不出来: info.AddField("Content", "content", db.Text ). FieldJoin(types.Join{ Table: "articles_content", Field: "id", JoinField: "article_id", })

打印sql语句是这样的: {"statement": "select articles.author_id,articles.author_name,articles.short_title,articles.title,articles.source,articles.pic_url,articles.pic_type,articles.status,articles.is_top,articles.sort_index,articles.id,group_concat(articles_content.content separator 'Z5dDrivernSKR') as articles_content_goadmin_join_content from articles left join articles_content on articles_content.article_id = articles.id GROUP BY articles.id order by articles.id desc LIMIT ? OFFSET ?", "args": [10,0]}

发现其中有这句话: group_concat(articles_content.content separator 'Z5dDrivernSKR') as articles_content_goadmin_join_content 这个 和 实际的 content 字段是不匹配的

我在 info.AddField("Content", "content", db.Text ). FieldJoin(types.Join{ Table: "articles_content", Field: "id", JoinField: "article_id", }) 后面加了 .FieldSubstr(0,30) 就不能正常显示,去掉这句话,在列表中能正常显示

moon-hzq avatar Nov 04 '20 09:11 moon-hzq

进入详情页后,也无法正常显示出 content,即使加了这样的代码 detail := articles.GetDetail() detail.AddField("Content", "content", db.Text ). FieldJoin(types.Join{ Table: "articles_content", Field: "id", JoinField: "article_id", }) 也不行

moon-hzq avatar Nov 04 '20 09:11 moon-hzq

detail.AddField("Content", "content", db.Text ).
后面的 db.Text 是错误的,这里填写的是 detail 表 id 的 data type

如果没理解错,下面的代码应该可以工作
detail.AddField(DISPLAY_NAME, "content", db.Bigint).
	FieldJoin(types.Join{
		Table:     "articles_content",
		JoinField: "article_id",
		Field:     "id",
	})

Lifelong-Study avatar Nov 08 '20 15:11 Lifelong-Study

Add following FieldDisplay(func(value types.FieldModel) interface{} { return fmt.Sprintf("%v", value.Row["articles_content_goadmin_join_content"]) })

Egor86 avatar Dec 16 '20 11:12 Egor86

Thank @Egor86 you saved me time! Xiexie

malparty avatar Feb 18 '22 12:02 malparty