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

Postgres issue with BelongsToMany #382

Closed
scottybo opened this issue Jan 10, 2019 · 3 comments
Closed

Postgres issue with BelongsToMany #382

scottybo opened this issue Jan 10, 2019 · 3 comments

Comments

@scottybo
Copy link

scottybo commented Jan 10, 2019

We use Laravel Nova for automatically generating admin screens and it supports Models that have BelongToMany relationships. However, an error is thrown when a BelongsToMany intermediate table doesn't contain an id column. This is because Postgres expects an id column to exist. (I've closed a ticket related to this here: #374)

The permissions table in Bouncer doesn't have an id column, and so when attempting to use Nova to manage Bouncer data, we get shown an error.

This is easily solved by adding an auto-incrementing id column to the Permissions table in Bouncer.

However, when we now try to call a Bouncer method such as Bouncer::allow($role)->to('do_something'); we get an error message SQLSTATE[42702]: Ambiguous column: 7 ERROR: column reference "id" is ambiguous.

The solution is pretty simple once again, we just need to specify the table in the getAssociatedAbilityIds function in /src/Conductors/Concerns/AssociatesAbilities.php so that any references to id become abilities.id.

    protected function getAssociatedAbilityIds(Model $authority, array $abilityIds, $forbidden)
    {
        
        $relation = $authority->abilities();

        $relation->whereIn('abilities.id', $abilityIds)->wherePivot('forbidden', '=', $forbidden);

        Models::scope()->applyToRelation($relation);

        return $relation->get(['abilities.id'])->pluck('abilities.id')->all();
    }

I appreciate this might be outside the scope of what is considered a bug as we are adding additional columns to the Bouncer permissions table, but if modifying the above function would not cause any problems, would it be possible to put this in place?

@scottybo
Copy link
Author

@JosephSilber Would it be possible to let me know your thoughts? Just closing doesn't give me much to go on :)

@JosephSilber
Copy link
Owner

I didn't "just close it". It was automatically closed by the commit that implemented it :)

@scottybo
Copy link
Author

Ohhhhh!!! Sorry, being stupid!

Thank you :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants