lucid icon indicating copy to clipboard operation
lucid copied to clipboard

MSSQL Issue

Open alexblunck opened this issue 9 years ago • 10 comments

I'm trying to run migrations on an Azure SQL database using the "mssql" client.

I've managed to correctly set up config to connect to the database but am getting following error when running ./ace migration:run

RequestError: select [name], [name] from [adonis_schema] order by [name] asc - Ambiguous column name 'name'.

alexblunck avatar Dec 17 '16 16:12 alexblunck

Can u share the migrations file?

thetutlage avatar Dec 20 '16 10:12 thetutlage

Sorry for the late response. I just set up a fresh adonis project and used the auth:setup ace command create the default auth migration files. Running the migration works fine on sqlite & mysql databases but fails on mssql (azure).

The adonis_schema table is actually created, but the "RequestError" seems to indicate that there is a problem when trying to query the adonis_schema table before running the other migrations.

alexblunck avatar Dec 21 '16 19:12 alexblunck

Running following sql query manually works fine by the way:

select [name] from [adonis_schema] order by [name] asc

Is knex generating a wrong query?

alexblunck avatar Dec 21 '16 20:12 alexblunck

I did some digging and it looks like this "behavior is by design" in MSSQL (Source).

The error is caused by the ORDER BY part of the query requiring an unambiguous column reference.

Using an alias for the name column in the problem query seems to be the fix.

If you change the query in src/Migrations/Mixins/Migrate.js's _getMigratedFiles method to following the query runs successfully on MSSQL:

Migrate._getMigratedFiles = function () {
  return this.database.select('name as name_column').from(this.migrationsTable).orderBy('name_column').pluck('name')
}

The _getFilesTillBatch method needs the same change as well.

I've tried this with mysql & sqlite and it seems to work fine there as well. I'm note sure if this has any other implications, but this might be the simple fix.

alexblunck avatar Dec 21 '16 21:12 alexblunck

Thanks for finding that out. I will add MSSQL to the testing list and will this issue too.

thetutlage avatar Dec 22 '16 06:12 thetutlage

it doesn't work for now RequestError: select [name] as [name], [name] from [adonis_schema] order by [name] asc - Ambiguous column name 'name'.

tanagorns avatar Jan 24 '17 09:01 tanagorns

Are you trying it from Github? A release is yet to go on npm

thetutlage avatar Jan 24 '17 09:01 thetutlage

yes, I have tried from github.

I have fixing code by

return this.database.select('name as aname').from(this.migrationsTable).orderBy('aname').pluck('name')

It's working

tanagorns avatar Jan 24 '17 09:01 tanagorns

i can confirm that the problem is that your [ as name ] fix in commit: fix(migrations): select column as {name}

is not working. The as name has to be something diffrent than name ;-) otherwise it is the same problem as before.

BR, Kevin

KevinGruber avatar Feb 14 '17 08:02 KevinGruber

The change actually broke the regression for Oracle

{ method: 'pluck',
  options: {},
  timeout: false,
  cancelOnTimeout: false,
  bindings: [],
  __knexQueryUid: '54b49e9c-7924-44ca-9dd8-1a614b2e709d',
  sql: 'select "name" "name", "name" from "adonis_schema" order by "name" asc',
  pluck: 'name' }
Error: select "name" "name", "name" from "adonis_schema" order by "name" asc - ORA-00960: ambiguous column naming in select list

use as aname and order aname does fix it

3dd13 avatar Sep 01 '17 07:09 3dd13