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

btree_gist constraint example #6976

Open
jlengelsen opened this issue Apr 18, 2024 · 0 comments
Open

btree_gist constraint example #6976

jlengelsen opened this issue Apr 18, 2024 · 0 comments
Assignees
Labels
type:question general questions

Comments

@jlengelsen
Copy link

Your Question

I'm trying to implement a btree_gist constraint with gorm struct tags. It should check that there are no overlapping date ranges for each individual foreign key reference that is not soft deleted. I've read the docs about database indexes but I didn't manage to get the tag right.

These are SQL statements that provide a basic example (the constraint in question is "chk_overlapping_dates"):

CREATE EXTENSION btree_gist;

CREATE TABLE "other_table" (
  "id" bigserial NOT NULL,
  "created_at" timestamptz NULL,
  "updated_at" timestamptz NULL,
  "deleted_at" timestamptz NULL,
  PRIMARY KEY ("id")
);

CREATE TABLE "my_table" (
  "id" bigserial NOT NULL,
  "created_at" timestamptz NULL,
  "updated_at" timestamptz NULL,
  "deleted_at" timestamptz NULL,
  "other_id" bigint NOT NULL,
  "valid_from" timestamptz NOT NULL,
  "valid_until" timestamptz NOT NULL,
  PRIMARY KEY ("id"),
  CONSTRAINT "fk_other_table" FOREIGN KEY ("other_id") REFERENCES "other_table" ("id"),
  CONSTRAINT "chk_valid_until" CHECK (valid_until > valid_from)
);

ALTER TABLE "my_table"
ADD CONSTRAINT "chk_overlapping_dates" EXCLUDE USING gist (
    other_id WITH =,
    tstzrange(valid_from, valid_until) WITH &&
  )
WHERE (deleted_at IS NULL);

Could you provide an example for how I could achieve that with gorm tags?

The document you expected this should be explained

https://gorm.io/docs/indexes.html

Expected answer

An example that implements the constraint in question with gorm struct tags.

@jlengelsen jlengelsen added the type:question general questions label Apr 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:question general questions
Projects
None yet
Development

No branches or pull requests

2 participants