MedicOneSystems / livewire-datatables

Advanced datatables using Laravel, Livewire, Tailwind CSS and Alpine JS
https://livewire-datatables.com/
MIT License
1.19k stars 259 forks source link

Ambiguous column name in pagination #362

Open gallomaf opened 2 years ago

gallomaf commented 2 years ago

Hello, I'm using datatables with province, region and nation. In province table, it don't work because of same coloumn name in the tables.

How I could solve?

SQLSTATE[42000]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Ambiguous column name 'nome'. (SQL: select * from (select [provincia].[id] as [id], [nazione].[nome] as [nazione.nome], [regione].[nome] as [regione.nome], [provincia].[nome] as [nome], [provincia].[attiva] as [attiva], CONCAT_WS('|**lwdt**|' ,COALESCE(provincia.id, ''), COALESCE(provincia.nome, ''), COALESCE(provincia.sigla, '')) AS 'callback_1172756607', row_number() over (order by "nome" asc) as row_num from [provincia] left join [regione] on [regione].[id] = [provincia].[id_regione] left join [nazione] on [nazione].[id] = [provincia].[id_nazione]) as temp_table where row_num between 26 and 50 order by row_num) (View: C:\Program Files (x86)\EasyPHP-Devserver-17\eds-www\sweet0\resources\views\livewire\datatables\datatable.blade.php)

Thank You so much

gzai commented 2 years ago

because you only show your error not your code, you can change this :

before :  

[provincia].[nome] as [nome],
[provincia].[attiva] as [attiva],
...
row_number() over (order by "nome" asc) as row_num

after : 

[provincia].[nome] as [provincia.nome],
[provincia].[attiva] as [provincia.attiva],
...
row_number() over (order by "provincia.nome" asc) as row_num
gallomaf commented 2 years ago

thank you, but I don't write the code: I use the following; in blade:

<livewire:tables.province exportable searchable="provincia.nome,provincia.sigla,provincia.id" sort="provincia.sigla|asc" per-page="25" />

This was my class that exted Datatable. I can change names of coloumns?

`class Province extends LivewireDatatable { public function builder() { return Provincia::query() ->leftJoin('regione', 'regione.id', 'provincia.id_regione') ->leftJoin('nazione', 'nazione.id', 'provincia.id_nazione'); }

public function columns()
{
    return [

        NumberColumn::name('id')
            ->label('ID')
            ->filterable(),

        Column::name('nazione.nome')
            ->label('Nazione')
            ->filterable(),

        Column::name('regione.nome')
            ->label('Regione')
            ->filterable(),

        Column::name('nome')
            ->label('Nome')
            ->defaultSort('asc')
            ->filterable(),

        BooleanColumn::name('attiva')
            ->label('Attiva')
            ->filterable(),

        Column::callback(['id','nome','sigla'], function ($id, $name, $sigla) {
            return view('livewire.components.table-actions', ['id' => $id, 'nome' => $name, 'sigla' => $sigla]);
        })->unsortable()
    ];

}

}`

thank you in advance