New rule: check constraints can not have subqueries

Open
#1,226 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
58/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Quiet
Tech stack
postgresql, rust, sql
Domain
databases, tooling

Research direction

No files, tests, or entry points are named in the issue. Locate the rule implementation and parser tests for PostgreSQL CHECK constraints in Squawk, then confirm that a subquery inside a CHECK constraint is reported and decide whether the diagnostic should recommend triggers.

Written by the indexing model from the issue text.

Description

enhancement

Context:
Just found out that subqueries can not be used in the check constraint.
Squawk linter does not complain but Postgresql throws the following error:
ERROR: cannot use subquery in check constraint (SQLSTATE 0A000)

Example:

ALTER TABLE
	table_x
ADD
	CONSTRAINT bar_should_equal_to_banana CHECK (
		(
			SELECT
				bar
			FROM
				table_y
			WHERE
				table_y.pk = table_x.y_pk
		) = 'banana'
	);

Expected fix:
New rule should throw an error on subqueries used in the check constraint.
Should suggest to use triggers instead?

Dominant language
Rust
Stars
1.2k
Forks
70
Avg merge
52m
Merged PRs (30d)
47

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from sbdchd/squawk

All issues in sbdchd/squawk

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.