From 3b07ed1b97691be1575825039b351ab9e1b1c397 Mon Sep 17 00:00:00 2001 From: toutou_o <33993460+kurimi1@users.noreply.github.com> Date: Thu, 24 Feb 2022 13:39:31 +0800 Subject: [PATCH] feat: support pg serial type for auto_increment (#1563) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * add correct example for pg's url * 🐞 fix: merge * 🐞 fix: pg default port * ✨ feat: support serial type Co-authored-by: kurimi1 --- tools/goctl/goctl.go | 2 +- tools/goctl/model/sql/model/postgresqlmodel.go | 12 +++++++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/tools/goctl/goctl.go b/tools/goctl/goctl.go index 304387c7..6cc7869c 100644 --- a/tools/goctl/goctl.go +++ b/tools/goctl/goctl.go @@ -690,7 +690,7 @@ var commands = []cli.Command{ Flags: []cli.Flag{ cli.StringFlag{ Name: "url", - Usage: `the data source of database,like "postgres://root:password@127.0.0.1:54332/database?sslmode=disable"`, + Usage: `the data source of database,like "postgres://root:password@127.0.0.1:5432/database?sslmode=disable"`, }, cli.StringFlag{ Name: "table, t", diff --git a/tools/goctl/model/sql/model/postgresqlmodel.go b/tools/goctl/model/sql/model/postgresqlmodel.go index ad20ee49..16d8813f 100644 --- a/tools/goctl/model/sql/model/postgresqlmodel.go +++ b/tools/goctl/model/sql/model/postgresqlmodel.go @@ -120,9 +120,15 @@ func (m *PostgreSqlModel) getColumns(schema, table string, in []*PostgreColumn) isNullAble = "NO" } - extra := "auto_increment" - if e.IdentityIncrement.Int32 != 1 { - extra = "" + extra := "" + // when identity is true, the column is auto increment + if e.IdentityIncrement.Int32 == 1 { + extra = "auto_increment" + } + + // when type is serial, it's auto_increment. and the default value is tablename_columnname_seq + if strings.Contains(e.ColumnDefault.String, table+"_"+e.Field.String+"_seq") { + extra = "auto_increment" } if len(index[e.Field.String]) > 0 {