[gogf/gf]orm WherePri与FindOne,都出错

2024-07-09 151 views
6

dao.User.Ctx(ctx).WherePri(1) 与 dao.User.Ctx(ctx).FindOne(1)

生成的sql都是: select from user where (1), 希望是:select from user where (id=1) 我用的是postgresql, 表也有定义主键,dao是用gf-cli自动生成的,gogf的版本是16.4

回答

8

可能是postgresql,没获取到主键;

可以提供下表结构和生成的dao,并且把

SELECT a.attname AS field, t.typname AS type FROM pg_class c, pg_attribute a
LEFT OUTER JOIN pg_description b ON a.attrelid=b.objoid AND a.attnum = b.objsubid,pg_type t
WHERE c.relname = '{表名}' and a.attnum > 0 and a.attrelid = c.oid and a.atttypid = t.oid
ORDER BY a.attnum`

替换下表名,这个sql的查询结果也发下

8

相关资料如下: 表结构: create table pm.family ( id int8 not null, pid int8 not null, pid_path varchar(200) not null, name varchar(50) not null, code varchar(50) not null, dsc varchar(200) not null, status varchar(50) not null, create_oper int8 not null, create_time timestamptz not null, update_oper int8 not null, update_time timestamptz not null, domain_id int8 not null, constraint PK_FAMILY primary key (id) );

create index Index_77 on pm.family ( domain_id );

生成的dao: // ========================================================================== // Code generated by GoFrame CLI tool. DO NOT EDIT. // ==========================================================================

package internal

import ( "github.com/gogf/gf/database/gdb" "github.com/gogf/gf/frame/g" "github.com/gogf/gf/frame/gmvc" )

// FamilyDao is the manager for logic model data accessing and custom defined data operations functions management. type FamilyDao struct { gmvc.M // M is the core and embedded struct that inherits all chaining operations from gdb.Model. C familyColumns // C is the short type for Columns, which contains all the column names of Table for convenient usage. DB gdb.DB // DB is the raw underlying database management object. Table string // Table is the underlying table name of the DAO. }

// FamilyColumns defines and stores column names for table family. type familyColumns struct { Id string // Pid string // PidPath string // Name string // Code string // Dsc string // Status string // CreateOper string // CreateTime string // UpdateOper string // UpdateTime string // DomainId string // }

// NewFamilyDao creates and returns a new DAO object for table data access. func NewFamilyDao() *FamilyDao { columns := familyColumns{ Id: "id", Pid: "pid", PidPath: "pid_path", Name: "name", Code: "code", Dsc: "dsc", Status: "status", CreateOper: "create_oper", CreateTime: "create_time", UpdateOper: "update_oper", UpdateTime: "update_time", DomainId: "domain_id", } return &FamilyDao{ C: columns, M: g.DB("pm").Model("family").Safe(), DB: g.DB("pm"), Table: "family", } } ,SQL结果: "id" "int8" "pid" "int8" "pid_path" "varchar" "name" "varchar" "code" "varchar" "dsc" "varchar" "status" "varchar" "create_oper" "int8" "create_time" "timestamptz" "update_oper" "int8" "update_time" "timestamptz" "domain_id" "int8"

9

看了下pgsql代码好像没有主键key设置,建议先用 findOne("id",1) mysql: image pgsql: image

@gqcn

3

若能自动识别主键,还是很方便的,希望下个版本可以用上

9

pgsql可以用这个sql

SELECT c.oid,a.attname AS field, t.typname AS type,a.attnotnull as null,
    (case when d.contype is not null then 'pri' else '' end)  as key
      ,ic.column_default as default_value,b.description as comment
      ,coalesce(character_maximum_length, numeric_precision, -1) as Length
      ,numeric_scale as Scale
FROM pg_attribute a
         left join pg_class c on a.attrelid = c.oid
         left join pg_constraint d on d.conrelid = c.oid and a.attnum = d.conkey[1]
         left join pg_description b ON a.attrelid=b.objoid AND a.attnum = b.objsubid
         left join  pg_type t ON  a.atttypid = t.oid
         left join information_schema.columns ic on ic.column_name = a.attname and ic.table_name = c.relname
WHERE c.relname = 'family' and a.attnum > 0
ORDER BY a.attnum;
8

image