MedicOneSystems / MedicOneSystems/livewire-datatables

[SQLSRV] Export checked records fails

Open
#443 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
PHP
Stars
1.2k
Forks
257
PR merge metrics
No merged PRs in 30d

Description

Using SQLSRV model and trying to export selected records to any format I get the following error:

SQLSTATE[42S22]: [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Invalid column name 'checkbox_attribute'.

Generated query (simplified):

SELECT
    [dba].[mytable].[myunique] AS [checkbox_attribute],
    [dba].[mytable].[myunique] AS [myunique],
    [dba].[anothertable].[data] AS [data]
FROM [dba].[mytable]
LEFT JOIN [dba].[anothertable] ON [dba].[mytable].[someid] = [dba].[anothertable].[someid]
HAVING checkbox_attribute IN (_62V10CVJJ)
ORDER BY dba.mytable.myunique DESC

As I found out, SQLSRV has a bit different order of operations, so it doesn't know about checkbox_attribute alias:

  1. FROM
  2. WHERE
  3. GROUP BY
  4. HAVING <-- doesn't know about SELECT alias
  5. SELECT expressions (like x AS y)
  6. DISTINCT
  7. UNION
  8. ORDER BY
  9. TOP

Quick and dirty workaround would be to wrap query into subquery like this:

SELECT * FROM (
SELECT
    [dba].[mytable].[myunique] AS [checkbox_attribute],
    [dba].[mytable].[myunique] AS [myunique],
    [dba].[anothertable].[data] AS [data]
FROM [dba].[mytable]
LEFT JOIN [dba].[anothertable] ON [dba].[mytable].[someid] = [dba].[anothertable].[someid]
ORDER BY dba.mytable.myunique DESC
) tempdb
WHERE checkbox_attribute IN (_62V10CVJJ)

I'm not sure how to deal with it in simplified way as it would require maintaining different code for different DB engines. Anyway, I'm open to do the testing.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no source file or test. Start by reproducing selected-record export with SQLSRV and trace the query generation that produces HAVING checkbox_attribute. Done means exports work on SQL Server without invalid-column errors while preserving behavior for other database engines.

Written by the indexing model from the issue text.

Assessment

Tech stack
laravel, php, sql
Domain
databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.