Skip to content

Add unique constraint option to hasMany fields #3

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Open
pborissow opened this issue Jan 16, 2025 · 0 comments
Open

Add unique constraint option to hasMany fields #3

pborissow opened this issue Jan 16, 2025 · 0 comments

Comments

@pborissow
Copy link
Contributor

Consider the following model:

    UserGroup: {
        fields: [
            {name: 'name',          type: 'string', unique: true, required: true},
            {name: 'description',   type: 'string'}
        ],
        hasMany: [
            {model: 'User',         name: 'users'}
        ]
    }

This will create a table like this:

CREATE TABLE USER_GROUP_USER (
    USER_GROUP_ID BIGINT NOT NULL,
    USER_ID BIGINT NOT NULL,
    CONSTRAINT FK_USER_GROUP_USER FOREIGN KEY (USER_GROUP_ID) REFERENCES USER_GROUP(ID)
        ON DELETE CASCADE ON UPDATE NO ACTION
);

There is no unique constraint for users in the user group table.

We should add support for a new "unique" keyword option to hasMany fields like this:

       ...
        hasMany: [
            {model: 'User',   name: 'users',   unique: true}
        ]
    ...

With the "unique" keyword we should create a unique index like this:

CREATE unique INDEX IDX_USER_GROUP_USER ON USER_GROUP_USER(USER_GROUP_ID, USER_ID);
@javaxt-project javaxt-project locked and limited conversation to collaborators Jan 16, 2025
# for free to subscribe to this conversation on GitHub. Already have an account? #.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant