Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

goctl model pg, numeric data type of PostgreSQL always treated as bigint, config not work #4624

Open
pandajc opened this issue Feb 5, 2025 · 0 comments

Comments

@pandajc
Copy link

pandajc commented Feb 5, 2025

Describe the bug
numeric data type of PostgreSQL has been treated as bigint, no matter what config I use, the config not worked

To Reproduce

goctl version 1.7.5

PostgreSQL DDL

CREATE TABLE balance_test_tab (
	id bigserial NOT NULL,
	balance numeric(78) DEFAULT 0 NOT NULL,
	CONSTRAINT balance_test_tab_pk PRIMARY KEY (id)
);

goctl.yaml

model:
  types_map:
    bigint:
      null_type: sql.NullInt64
      type: int64
      unsigned_type: uint64
    numeric:
      null_type: "*big.Int"
      type: "*big.Int"
      unsigned_type: "*big.Int"
      pkg: math/big

result

BalanceTestTab struct {
		Id      int64 `db:"id"`
		Balance int64 `db:"balance"`
}

Expected behavior
the Balance should be *big.Int, but it is generated a int64 field

And if I modify the bigint types_map, fields id bigserial and balance numeric(78) both be generated to *big.Int, which indicated that numeric data type of PostgreSQL is always treated as bigint

model:
  types_map:
    bigint:
      null_type: "*big.Int"
      type: "*big.Int"
      unsigned_type: "*big.Int"
      pkg: math/big
BalanceTestTab struct {
		Id      *big.Int `db:"id"`
		Balance *big.Int `db:"balance"`
}

Logic in source code
I found in
https://github.com/zeromicro/go-zero/blob/master/tools/goctl/model/sql/model/postgresqlmodel.go

there is a function convertPostgreSqlTypeIntoMysqlType and variable p2m who convert numeric of postgreSQL to bigint of MySQL, I think it is not reasonable, numeric of postgreSQL is almost equal to decimal of MySQL, both are used for accurate large number, so the value of key "numeric" in var p2m should be "decimal"

source code:

func (m *PostgreSqlModel) convertPostgreSqlTypeIntoMysqlType(in string) string {
	r, ok := p2m[strings.ToLower(in)]
	if ok {
		return r
	}

	return in
}
var p2m = map[string]string{
	"int8":        "bigint",
	"numeric":     "bigint",
	"float8":      "double",
	"float4":      "float",
	"int2":        "smallint",
	"int4":        "integer",
	"timestamptz": "timestamp",
}

Screenshots
Image

Environments:

  • goctl version [1.7.5]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant