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

BelongsToMany relationship with orderBy returns duplicate column name created_at #2705

Closed
dragonfly4 opened this issue Oct 31, 2021 · 5 comments

Comments

@dragonfly4
Copy link

Summary of problem or feature request

Displaying list of records from Pivot Table works. Basic example with a belongsToMany in the model.

$model->abilities();
    public function abilities()
    {
        return $this->belongsToMany(Ability::class);
    }

Add orderBy results in duplicate column error. The Pivot Table has timestamp columns.

$model->abilities()->orderBy('ability.name');

error: "Exception Message:\n\nSQLSTATE[42S21]: Column already exists: 1060 Duplicate column name 'created_at' (SQL: select count(*) as aggregate from (select * from abilities inner join ability_role on abilities.id = ability_role.ability_id where ability_role.role_id = 2 order by abilities.name asc) count_row_table)"

The same command executes properly in Tinkerwell / Laravel Controller. But not via laravel-datatables. Maybe related to #1739 ?

Quick fix for me was to add a select to exclude the created_at + updated_at columns.

$model->abilities()->select('id','name','label')->orderBy('ability.name');

System details

  • Operating System: WAMPSERVER
  • PHP Version: 8.0.11
  • Laravel Version: 8.68.1
  • Laravel-Datatables Version: 9.18.2
@yajra
Copy link
Owner

yajra commented Feb 1, 2022

@dragonfly4 sorry for late response. I think this PR #2721 will fix the issue. Would you be able to review and give some feedbacks? Thanks!

@dragonfly4
Copy link
Author

@yajra Thanks for your reply!

I tested that but it's not fixing the issue above. Same error.

Do you want me to share anything else please?

@yajra
Copy link
Owner

yajra commented Feb 1, 2022

Let's continue the discussion in the PR. Thanks!

@yajra yajra closed this as completed Feb 1, 2022
@yajra yajra reopened this Feb 1, 2022
@github-actions
Copy link

This issue is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@github-actions github-actions bot added the stale label Oct 22, 2022
@github-actions
Copy link

This issue was closed because it has been inactive for 7 days since being marked as stale.

@github-actions github-actions bot locked and limited conversation to collaborators Nov 14, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants