Skip to content

UniqueConstrain isn't respected on ActionRoles #188

Description

@zannkukai

Package version (if known): 1.4.2

Describe the bug

It is possible to register multiple ActionRoles with same values despite the SQLAlchemy UniqueConstraint (https://github.com/inveniosoftware/invenio-access/blob/master/invenio_access/models.py#L115-L118) if argument is None.

Steps to Reproduce

access_actionsroles table initial state

image

from invenio_access import action_factory, ActionRoles
from invenio_accounts.models import Role
from invenio_db import db
import random

random_role = random.choice(Role.query.all())

action = action_factory('my-action')
action_arg = action_factory('my-action-arg', parameter=True)

for i in range(3):
    db.session.add(ActionRoles.allow(action, role=random_role))
    db.session.commit()

for i in range(3):
    try:     
        db.session.add(ActionRoles.allow(action_arg('argValue'), role=random_role))
        db.session.commit()
    except Exception as e:
        print('Error', str(e))  # You should see 2 SQLAlchemy errors

access_actionroles table after the script
image

Expected behavior

The UniqueConstraint key should be respected even if action doesn't have any argument.
Regarding this StackOverflow answer, it should be possible using 2 indexes (one if argument is null, second if argument is not null) : https://stackoverflow.com/a/57647018

Additional context

It should be the same for ActionUsers and ActionSystemRoles

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