activerecord-sqlserver-adapter icon indicating copy to clipboard operation
activerecord-sqlserver-adapter copied to clipboard

The column prefix does not match with a table name or alias name used in the query.

Open papillon opened this issue 15 years ago • 26 comments

Hi,

I am working with a legacy database that exposes a few problems with the sqlserver adapter (confirmed with version 3.0.9). One of them is the following:

In my models, I have to use set_table_name like this:

class Header < ActiveRecord::Base
  set_table_name table_name_prefix + "headers"
end

(the table_name_prefix is set in config/application.rb with config.active_record.table_name_prefix = "NORTHWIND$")

This results in the following SQL Server (2005) error message: The column prefix 'NORTHWIND$headers' does not match with a table name or alias name used in the query.

The SQL statement that leads to this error is:

SELECT TOP (20) [NORTHWIND$headers].* FROM 
( SELECT ROW_NUMBER() OVER (ORDER BY no ASC) AS [__rn], [NORTHWIND$headers].* FROM 
  [NORTHWIND$headers] WHERE ((document_type = 1) AND (closed = 0 AND support_completion = 0))
) 
AS [__rnt] WHERE [__rnt].[__rn] > 0

It seems SQL Server does not like the [NORTHWIND$headers].* select clause. It works if I remove the table name and just select all columns with * I have modified sqlserver.rb to illustrate what I mean:

  def visit_Arel_Nodes_SelectStatementWithOffset(o)
    orders = rowtable_orders(o)
    [ "SELECT",
      (visit(o.limit) if o.limit && !single_distinct_select_statement?(o)),
      "*", #(rowtable_projections(o).map{ |x| visit(x) }.join(', ')),  #### THIS LINE ####
      "FROM (",
        "SELECT ROW_NUMBER() OVER (ORDER BY #{orders.map{ |x| visit(x) }.join(', ')}) AS [__rn],",
        visit_Arel_Nodes_SelectStatementWithOutOffset(o,true),
      ") AS [__rnt]",
      (visit(o.offset) if o.offset),
    ].compact.join ' '
  end

Unfortunately, I do not have enough experience / insight into Arel to do anything against this issue. Could some developer please have a look at this problem or give some hints ?

Thanks a lot, Ingmar

papillon avatar Feb 21 '11 15:02 papillon

Can you explain what NORTHWIND$ is? A schema name?

metaskills avatar Feb 21 '11 16:02 metaskills

Yes, it's the way of the legay application to represent multiple companies within one database. It's similar to the table prefix of e.g. phpbb.

Note: With the change to sqlserver.rb the application runs fine. It's just that I can not provide a real patch because I do not understand enough of the code.

papillon avatar Feb 21 '11 19:02 papillon

Then why are you using it with out a dot? Typically schemas prefixes are like this. "HumanResources." So that way when you have a table via reflection, it get's put together like this. "HumanResources.employees". Curious?

metaskills avatar Feb 21 '11 19:02 metaskills

I do not have a choice, unfortunately. The application uses the dollar sign and this can not be changed. Do you think this ($ instead of .) has an impact here ?

papillon avatar Feb 21 '11 21:02 papillon

It sounds like you are trying to do something incredible odd or straight forward and you have the wrong assumptions how to get it done.

The end point is that "NORTHWIND$" is not a schema if there is not "." after it. Maybe that is something like a crazy var or something. But everything I know about SQL Server says you can not have a schema prefix without the dot. Your going to have to give me much more to go on.

metaskills avatar Feb 21 '11 21:02 metaskills

I would agree, you are right (although I am not an expert regarding schemas). The legacy database is odd but I've been successfully using it for the last 4 years with Rails. What can I say, real life sometimes is just not... ideal.

The point is, however, that NORTHWIND$ is not trying to be a schema at all. It's treated as part of the table name by SQL Server, so a typical query could look like this:

select * from [NORTHWIND$my table] 

SQL Server would treat it exactly the same if the name was northwind_my table, i.e. the query would be in this case

select * from [northwind_my table]

$ is just a valid character in the table name, nothing more. Now, if you are telling me that using rails table_name_prefix is the wrong way in this case, I should be able to find a different solution.

papillon avatar Feb 21 '11 22:02 papillon

OK, you did say schema. It is important to know what we are talking about and if this is not a schema and is a prefix, then let's stick with that.

Can you explain what you mean by [NORTHWIND$my table] expanding to [northwind_my table]?

metaskills avatar Feb 21 '11 23:02 metaskills

Agreed.

It is just an example to illustrate the point that the $ is nothing special, it could be an underscore just the same.

papillon avatar Feb 22 '11 12:02 papillon

Well I am not seeing the problem then. The adapter is tested with ActiveRecord and we even have our own tests, some with prefixes and using limit/offset. All tests are passing. So you need to identify what is different in your setup and illustrate a problem. So far I am not seeing now. Please be detailed and do some more diligence and I can see if we can help. For example, to illustrate the problem, maybe show me things like this.

SELECT * FROM [NORTHWIND$headers] SELECT [NORTHWIND$headers].* FROM [NORTHWIND$headers]

These two queries from a SQL standpoint are exactly the same. If you run them in SMS/QA, they work right?

metaskills avatar Feb 22 '11 12:02 metaskills

Those two examples would work and are equivalent, agreed. But not the generated SQL command that I pasted in my first post. I ran it in SSMS, then pasted the resulting error message.

The problem might be that SQL Server does not see the table name, because it used only in the subquery. This statement seems to point into this direction:

"If a table appears only in a subquery and not in the outer query, then columns from that table cannot be included in the output (the select list of the outer query)." (cf. http://msdn.microsoft.com/en-us/library/ms189575.aspx)

Selecting all columns (*) works, however.

papillon avatar Feb 22 '11 12:02 papillon

Please setup a control and show me the SQL for a basic model that does.

BasicModel.limit(20).all

metaskills avatar Feb 22 '11 13:02 metaskills

This is the result: WebUser.limit(20).all => SELECT TOP 20 [NORTHWIND$web User].* FROM [NORTHWIND$web User]

This works fine. In contrast, this does not work:

WebUser.limit(20).all(:offset => 50)
=> The column prefix 'NORTHWIND$web User' does not match with a table name or alias name used in the query.: SELECT TOP (20) [NORTHWIND$web User].* FROM ( SELECT ROW_NUMBER() OVER (ORDER BY [NORTHWIND$web User].[id] ASC) AS [__rn], [NORTHWIND$web User].* FROM [NORTHWIND$web User] ) AS [__rnt] WHERE [__rnt].[__rn] > 50

(same error message as in my original post).

papillon avatar Feb 23 '11 13:02 papillon

You know, I think this might have nothing to do with the NORTHWIND$ prefix. Could it be that limit().offset() has a problem in other cases as well?

papillon avatar Mar 03 '11 16:03 papillon

I do know that as a rule of thumb you need to use an :order with any limit/offset pair. This is different that other DBs that allow you to return an ambiguous result. SQL Server does not let you do that. The adpater/visitor works to shim things up by finding the primary key of the table and using it as an order (implied by other DBs) and use it if no order was specified. Perhaps you are having an issue with that "help" going on in the background. Try adding an :order to things and let me know.

metaskills avatar Mar 03 '11 16:03 metaskills

The background magic does its thing and find the "id" column automatically (ORDER BY [NORTHWIND$web User].[id] in the example above). If I specify it explicitely, the generated SQL changes slightly:

WebUser.limit(20).offset(50).all(:order => "id asc")
SELECT TOP (20) * FROM ( SELECT ROW_NUMBER() OVER (ORDER BY id ASC) AS [__rn], [NORTHWIND$web User].* FROM [NORTHWIND$web User] ) AS [__rnt] WHERE [__rnt].[__rn] > 50

Now it issues a SELECT TOP (20) * FROM... instead of the former SELECT TOP (20) [NORTHWIND$web User].* FROM...

(note: this works fine)

papillon avatar Mar 03 '11 19:03 papillon

So, what would you say the next steps are ? Does this qualify as a bug, is it me, or shall we blame someone else ? :-)

papillon avatar Mar 07 '11 12:03 papillon

Show me a failing test and I will fix it. I suggest using the existing li It/offset sqlserver test file and if needed adding a supporting schema changes to the specific schema file.

metaskills avatar Mar 07 '11 12:03 metaskills

Given this test:

should 'support $ in table name' do
  StrangeTable.new.save
  StrangeTable.limit(20).offset(1).all
end

and this addition to sqlserver_specific_schema.rb:

create_table 'my$strange_table', :force => true do |t|
  t.column :number, :real
end

and this model (strange_table.rb):

class StrangeTable < ActiveRecord::Base
  set_table_name "my$strange_table"
end

produces this error:

1) Error: 
test: When selecting with limit and offset support $ in table name. (OffsetAndLimitTestSqlserver):
ActiveRecord::StatementInvalid: ODBC::Error: 37000 (107) [Microsoft][ODBC SQL Server Driver][SQL Server]The column prefix 'my$strange_table' does not match with a table name or alias name used in the query.:
SELECT TOP (20) [my$strange_table].* FROM ( SELECT ROW_NUMBER() OVER (ORDER BY [my$strange_table].[id] ASC) AS [__rn], [my$strange_table].* FROM [my$strange_table] ) AS [__rnt] WHERE [__rnt].[__rn] > (1)

(updated messed-up formatting)

papillon avatar Mar 07 '11 15:03 papillon

Can someone provide a tested diff/patch?

metaskills avatar Apr 27 '11 13:04 metaskills

I can't provide a patch right now. However, note that the problem also occurs when accessing tables from another database. In an ActiveRecord model I write set_table_name 'legacydb.dbo.department'. Then try

> Department.limit(20).offset(1).all

Department Load (8.7ms)  SELECT TOP (20) [legacydb].[dbo].[department].* FROM ( SELECT ROW_NUMBER() OVER (ORDER BY [legacydb].[dbo].[department].[id] ASC) AS [__rn], [legacydb].[dbo].[department].* FROM [legacydb].[dbo].[department] ) AS [__rnt] WHERE [__rnt].[__rn] > (1)

Error: The column prefix 'legacydb.dbo.department' does not match with a table name or alias name used in the query.: SELECT TOP (20) [legacydb].[dbo].[department].* FROM ( SELECT ROW_NUMBER() OVER (ORDER BY [legacydb].[dbo].[department].[id] ASC) AS [__rn], [legacydb].[dbo].[department].* FROM [legacydb].[dbo].[department] ) AS [__rnt] WHERE [__rnt].[__rn] > (1)

chudler avatar Jun 06 '11 18:06 chudler