something wrong when using xorm reverse on unique index #1183

Closed
opened 2019-01-03 10:14:18 +00:00 by xuyz · 5 comments
xuyz commented 2019-01-03 10:14:18 +00:00 (Migrated from github.com)

error message

[Error] reverse.go:178 Unknown col is_default) WHERE in index userc_addr_userc_id_is_default_key of table userc_addr, columns [id userc_id name phone city area detail is_default create_time area_id city_id]

the table

create table userc_addr(
  id bigserial primary key,
  userc_id bigint REFERENCES userc(id),
  name varchar(128),
  phone varchar(11),
  city varchar(128),
  area varchar(128),
  detail text,
  is_default boolean DEFAULT FALSE,
  create_time timestamp with time zone,
  area_id int,
  city_id int
);
CREATE UNIQUE index userc_addr_userc_id_is_default_key on userc_addr(userc_id, is_default) where is_default=true;
CREATE index userc_addr_userc_id_idx on userc_addr(userc_id);

error message ``` [Error] reverse.go:178 Unknown col is_default) WHERE in index userc_addr_userc_id_is_default_key of table userc_addr, columns [id userc_id name phone city area detail is_default create_time area_id city_id] ``` the table ``` create table userc_addr( id bigserial primary key, userc_id bigint REFERENCES userc(id), name varchar(128), phone varchar(11), city varchar(128), area varchar(128), detail text, is_default boolean DEFAULT FALSE, create_time timestamp with time zone, area_id int, city_id int ); CREATE UNIQUE index userc_addr_userc_id_is_default_key on userc_addr(userc_id, is_default) where is_default=true; CREATE index userc_addr_userc_id_idx on userc_addr(userc_id); ```
xuyz commented 2019-01-03 10:16:21 +00:00 (Migrated from github.com)

the sql of userc table

create table userc(
  id bigserial primary key,
  phone varchar(11),
  create_time timestamp with time zone,
  UNIQUE(phone)
);
the sql of userc table ``` create table userc( id bigserial primary key, phone varchar(11), create_time timestamp with time zone, UNIQUE(phone) ); ```
xuyz commented 2019-01-03 10:30:19 +00:00 (Migrated from github.com)

moved cmd

moved [cmd](https://github.com/go-xorm/cmd/issues/50)
xuyz commented 2019-01-28 07:51:43 +00:00 (Migrated from github.com)

I think the problem is here.

cs := strings.Split(indexdef, "(")
colNames = strings.Split(cs[1][0:len(cs[1])-1], ",")

when execute
SELECT indexname, indexdef FROM pg_indexes WHERE tablename='userc_addr' and schemaname='public';
got indexdef
CREATE UNIQUE INDEX userc_addr_userc_id_is_default_key ON public.userc_addr USING btree (userc_id, is_default) WHERE (is_default = true)

I can submit a pull request if necessary.

I think the problem is [here](https://github.com/go-xorm/xorm/blob/master/dialect_postgres.go#L1129,L1130). ``` cs := strings.Split(indexdef, "(") colNames = strings.Split(cs[1][0:len(cs[1])-1], ",") ``` when execute `SELECT indexname, indexdef FROM pg_indexes WHERE tablename='userc_addr' and schemaname='public';` got indexdef `CREATE UNIQUE INDEX userc_addr_userc_id_is_default_key ON public.userc_addr USING btree (userc_id, is_default) WHERE (is_default = true)` I can submit a pull request if necessary.
xuyz commented 2019-01-28 07:55:49 +00:00 (Migrated from github.com)
@lunny

@xuyz please send a PR and better if you can add some tests

@xuyz please send a PR and better if you can add some tests
Sign in to join this conversation.
No Milestone
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: xorm/xorm#1183
No description provided.