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

Issue with generated query when using non-default schema (!= dbo) on mssql

Open giosakti opened this issue 11 years ago • 2 comments

I'm having this issue when doing even a simple query like User.first on my development environment.

If I create my table with a custom schema:

class CreatePartyPartners < ActiveRecord::Migration
  def change
    execute <<-SQL
      CREATE SCHEMA Party
    SQL
    create_table('Party.Partners', primary_key: 'Id') do |t|
...

And create a model by manually set self.table_name

class Party::Partner < ActiveRecord::Base
  self.table_name = 'Party.Partners'
  self.primary_key = 'Id'
...

Firing up the rails console and type Party::Partner.first will result in an error like this:

jruby-1.7.12 :012 > Party::Partner.first
ActiveRecord::JDBCError: table: Party].[Partners does not exist
    from arjdbc/jdbc/RubyJdbcConnection.java:1098:in `columns'

This is the generated query of Party::Partner.limit(1)

SELECT t.* FROM ( SELECT ROW_NUMBER() OVER(ORDER BY Partners.Id) AS _row_num, [Party].[Partners].* FROM [Party].[Partners] ) AS t WHERE t._row_num BETWEEN 1 AND 1

If I remove the square brackets from FROM [Party].[Partners] like this FROM Party.Partners: the query will work normally

I'm using JRuby 1.7.12, Rails 4.1.0, activerecord-jdbcmssql-adapter 1.3.7 and sql server 2008 r2

you can support MS-SQL fixes at BountySource

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/2148127-issue-with-generated-query-when-using-non-default-schema-dbo-on-mssql?utm_campaign=plugin&utm_content=tracker%2F136963&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F136963&utm_medium=issues&utm_source=github).

giosakti avatar May 14 '14 02:05 giosakti

@giosakti thanks for the report(s) ... we'll pretty low on maintainers thus would recommend looking into this on your own. try setting up the tests against a test DB to get you started.

kares avatar May 15 '14 19:05 kares

Ok, thanks for the heads up :)

giosakti avatar May 16 '14 01:05 giosakti