iver-wharf / wharf-api

Wharf backend written in Go
MIT License
1 stars 0 forks source link

Rely on default constraint names #163

Closed applejag closed 2 years ago

applejag commented 2 years ago

Summary

Motivation

GORM apparently got confused when we specified the foreign key column name. It wrongly associated the Token.TokenID column instead of the Project.TokenID and Provider.TokenID etc.

This whole confusion seems to be because GORM was updated between wharf-api v5.0.0 and v5.1.0.

My love for GORM is decreasing more and more as time goes on. It's overall just quite unstable in its behaviour. This fight about foreign key constraint names we've fought before in a previous update. Now we tackle it again.

Before

$ \d token
                                          Table "public.token"
   Column   |           Type           | Collation | Nullable |                 Default
------------+--------------------------+-----------+----------+-----------------------------------------
 created_at | timestamp with time zone |           |          |
 updated_at | timestamp with time zone |           |          |
 token_id   | bigint                   |           | not null | nextval('token_token_id_seq'::regclass)
 value      | character varying(500)   |           | not null |
 user_name  | character varying(500)   |           | not null | ''::character varying
Indexes:
    "token_pkey" PRIMARY KEY, btree (token_id)
Foreign-key constraints:
    "fk_branch_token" FOREIGN KEY (token_id) REFERENCES branch(branch_id) ON UPDATE RESTRICT ON DELETE RESTRICT
    "fk_project_token" FOREIGN KEY (token_id) REFERENCES project(project_id) ON UPDATE RESTRICT ON DELETE RESTRICT
    "fk_provider_token" FOREIGN KEY (token_id) REFERENCES provider(provider_id) ON UPDATE RESTRICT ON DELETE RESTRICT

After

$ \d token
                                          Table "public.token"
   Column   |           Type           | Collation | Nullable |                 Default
------------+--------------------------+-----------+----------+-----------------------------------------
 created_at | timestamp with time zone |           |          |
 updated_at | timestamp with time zone |           |          |
 token_id   | bigint                   |           | not null | nextval('token_token_id_seq'::regclass)
 value      | character varying(500)   |           | not null |
 user_name  | character varying(500)   |           | not null | ''::character varying
Indexes:
    "token_pkey" PRIMARY KEY, btree (token_id)
Referenced by:
    TABLE "branch" CONSTRAINT "fk_branch_token" FOREIGN KEY (token_id) REFERENCES token(token_id) ON UPDATE RESTRICT ON DELETE RESTRICT
    TABLE "project" CONSTRAINT "fk_project_token" FOREIGN KEY (token_id) REFERENCES token(token_id) ON UPDATE RESTRICT ON DELETE RESTRICT
    TABLE "provider" CONSTRAINT "fk_provider_token" FOREIGN KEY (token_id) REFERENCES token(token_id) ON UPDATE RESTRICT ON DELETE RESTRICT

"Referenced by" instead of its own "Foreign-key constraints". Much better