Skip to content

Add unique constraint option to hasMany fields #3

Open
@pborissow

Description

@pborissow

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);

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions