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

goctl gen model with pg and cache leads to a column "xxx" does not exist error

Open akalittle opened this issue 1 year ago • 6 comments

Describe the bug I use the code generated by goctl model pg datasource -url="postgresql://postgres@localhost:5432/postgres?sslmode=disable" -table="xxx" -c

the ddl is below and there is a unique index

CREATE TABLE tx_status (
    id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
    network character varying(255) NOT NULL,
    hash text NOT NULL,
    status character varying NOT NULL,
    receipt jsonb NOT NULL DEFAULT '{}'::jsonb,
    "createAt" timestamp with time zone NOT NULL,
    "updateAt" timestamp with time zone NOT NULL
);


-- Indices -------------------------------------------------------


CREATE UNIQUE INDEX tx_network_hash_uniq_index ON tx_status(network text_ops,hash text_ops);

To Reproduce

  1. The code is

    	res, err := l.svcCtx.DB.FindOneByNetworkHash(l.ctx, network, in.Hash)
    
    
  2. The error is

    {"@timestamp":"2022-09-21T15:03:22.197+08:00","caller":"sqlx/utils.go:140","content":"stmt: select id,network,hash,status,receipt,createAt,updateAt from \"public\".\"tx_status\" where network = 'xxx' and hash = 'xxx' limit 1, error: pq: column \"createat\" does not exist","level":"error","span":"754988c3cdf31f51","trace":"d69c8f5d160d9b30dbab024f1b353bd4"}
    
    

It's strange while in the log the column is camelcase , but in the error message the column is lowercased.

Expected behavior no error Screenshots image

Environments (please complete the following information):

  • OS: macos 12.5.1
  • github.com/zeromicro/go-zero v1.4.1
  • goctl version 1.4.0 darwin/amd64

akalittle avatar Sep 21 '22 07:09 akalittle

try without quoting createAt and updateat field

a0v0 avatar Sep 21 '22 09:09 a0v0

try without quoting createAt and updateat field

Of course I can try. But I think it doesn't solve the problem. It's the code generated.

akalittle avatar Sep 21 '22 10:09 akalittle

The fields are from :

	txStatusFieldNames          = builder.RawFieldNames(&TxStatus{}, true)
	txStatusRows                = strings.Join(txStatusFieldNames, ",")

I can hack this time. But it's not the right way to solve this problem forever.

akalittle avatar Sep 21 '22 10:09 akalittle

This error also happens in FindOne function. So it's a critical bug ? The generated code don't work.

{"@timestamp":"2022-09-22T10:23:00.305+08:00","caller":"sqlx/utils.go:139","content":"stmt: select id,network,hash,status,receipt,createAt,updateAt from \"public\".\"tx_status\" where id = 16268 limit 1, error: pq: column \"createat\" does not exist","level":"error","span":"7cbb99e9939a30e0","trace":"ebdd9781527ac4e87500db7ea60e2d29"}

	test, err := l.svcCtx.DB.FindOne(l.ctx, 16268)
	fmt.Println("err test", err)
err test pq: column "createat" does not exist

If I change the ddl from createAt to create_at it works.

And I added a test field named uuuTest

{"caller":"sqlx/utils.go:139","
content":"stmt: select id,network,hash,status,receipt,uuuTest from \"public\".\"tx_status\" where network = 'x' and hash = 'x' limit 1, 
error: pq: column \"uuutest\" does not exist",
"level":"error","
span":"cfd58604191f622e",
"trace":"25b150fd5dcb7032a29dc9a2c8720a92"}
err pq: column "uuutest" does not exist

akalittle avatar Sep 22 '22 02:09 akalittle

Postsql is case sensitive, just change the uuutest column name to lowercase. https://stackoverflow.com/questions/20878932/are-postgresql-column-names-case-sensitive

MaxToby avatar Sep 22 '22 03:09 MaxToby

Postsql is case sensitive, just change the uuutest column name to lowercase. https://stackoverflow.com/questions/20878932/are-postgresql-column-names-case-sensitive

It's the right case.

select id,network,hash,status,receipt,uuuTest from \"public\".\"tx_status\" and then leads to an pq: column "uuutest" does not exist

I just want to know when it changed from uuuTest to uuutest

akalittle avatar Sep 22 '22 03:09 akalittle

Anyone follow this issue ?

akalittle avatar Sep 26 '22 01:09 akalittle

Hey, is there anyone following this issue ?

akalittle avatar Oct 11 '22 09:10 akalittle

See #2100

kesonan avatar Oct 21 '22 16:10 kesonan

See #2100

So what about the column uuuTest ?

akalittle avatar Oct 25 '22 01:10 akalittle