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

Allow for check constraints in shorthands #1183

Open
Apreche opened this issue Jun 3, 2024 · 0 comments
Open

Allow for check constraints in shorthands #1183

Apreche opened this issue Jun 3, 2024 · 0 comments
Labels
c: feature Request for new feature s: waiting for user interest Waiting for more users interested in this feature
Milestone

Comments

@Apreche
Copy link

Apreche commented Jun 3, 2024

Description

Consider this check constraint example from the postgres documentation.

CREATE TABLE products (
    product_no integer,
    name text,
    price numeric CONSTRAINT positive_price CHECK (price > 0)
);

You can make this table in node-pg-migrate like so:

pgm.createTable("mytable", {
    product_no: "integer",
    name: "text",
    price: {
        type: "numeric",
        check: "price > 0"
    }
});

But what if you wanted to have price fields with the same constraint on many tables? That's a perfect case for creating a shorthand for the price column.

exports.shorthands = {
    price: {
        type: "numeric",
        check: "price > 0"
    }
};

There's a huge problem here. What if someone wants to make a column of type price, but the column name isn't literally price?

pgm.createTable("mytable", {
    retail_price: "price"
});

This isn't going to work because you would need check: "retail_price > 0", but the shorthand just has price > 0

Suggested solution

When writing a check constraint in a shorthand, there should be some way to template the column name. I don't know what the syntax would be, this is just the first thing that came to mind without putting much thought into it.

exports.shorthands = {
    price: {
        type: "numeric",
        check: "${columnname} > 0"
    }
};

Alternative

Maybe the shorthands could be written using the shorthand, but then node-pg-migrate will intelligently replace the shorthand name in the constraint with the actual column name. Then this original example would actually work.

exports.shorthands = {
    price: {
        type: "numeric",
        check: "price > 0"
    }
};

It seems like this would be complicated and prone to danger, however, because you might have a situation like this

exports.shorthands = {
    price: {
        type: "numeric",
        check: "prices > 0"
    }
};

In which you do not want to replace price.

Additional context

No response

@Apreche Apreche added the s: pending triage Pending Triage label Jun 3, 2024
@Shinigami92 Shinigami92 added c: feature Request for new feature s: waiting for user interest Waiting for more users interested in this feature and removed s: pending triage Pending Triage labels Jun 3, 2024
@Shinigami92 Shinigami92 added this to the vAnytime milestone Jun 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c: feature Request for new feature s: waiting for user interest Waiting for more users interested in this feature
Projects
None yet
Development

No branches or pull requests

2 participants