Skip to content

Need to customize cast type of uniqueidentifier #658

@ArkieCoder

Description

@ArkieCoder

This might be working as designed, but in my use case, it's breaking my code. :)

I am running ruby scripts against a database of a 3rd party software package. I'm not able to change the schema.

I need to integrate with a DataTables.net front end that is completely divorced from the backend. Everything is working with DataTables except searching. When I enter search text, ActiveRecord generates the following SQL:

SELECT COUNT(*)
FROM [mtech].[ReportIndex]
WHERE (
    (
      (
        (
          (
            (
              (
                (
                  (
                    (
                      (
                        (
                          (
                            (
                              (
                                (
                                  (
                                    (
                                      (
                                        (
                                          (
                                            CAST([mtech].[ReportIndex].[CreationDate] AS VARCHAR) LIKE N'%B%'
                                            OR CAST([mtech].[ReportIndex].[ReportName] AS VARCHAR) LIKE N'%B%'
                                            )
                                          OR CAST([mtech].[ReportIndex].[IRN] AS VARCHAR) LIKE N'%B%'
                                          )
                                        OR CAST([mtech].[ReportIndex].[LastModDate] AS VARCHAR) LIKE N'%B%'
                                        )
                                      OR CAST([mtech].[ReportIndex].[ReplicationDateTime] AS VARCHAR) LIKE N'%B%'
                                      )
                                    OR CAST([mtech].[ReportIndex].[ReplicaSourceId] AS VARCHAR) LIKE N'%B%'
                                    )
                                  OR CAST([mtech].[ReportIndex].[ReportID] AS VARCHAR) LIKE N'%B%'
                                  )
                                OR CAST([mtech].[ReportIndex].[SystemFlag] AS VARCHAR) LIKE N'%B%'
                                )
                              OR CAST([mtech].[ReportIndex].[ReportGranularityMode] AS VARCHAR) LIKE N'%B%'
                              )
                            OR CAST([mtech].[ReportIndex].[BaseTableName] AS VARCHAR) LIKE N'%B%'
                            )
                          OR CAST([mtech].[ReportIndex].[ReportingClassDisplayName] AS VARCHAR) LIKE N'%B%'
                          )
                        OR CAST([mtech].[ReportIndex].[VersionNo] AS VARCHAR) LIKE N'%B%'
                        )
                      OR CAST([mtech].[ReportIndex].[BaseClassID] AS VARCHAR) LIKE N'%B%'
                      )
                    OR CAST([mtech].[ReportIndex].[ExternalId] AS VARCHAR) LIKE N'%B%'
                    )
                  OR CAST([mtech].[ReportIndex].[DeletedFlag] AS VARCHAR) LIKE N'%B%'
                  )
                OR CAST([mtech].[ReportIndex].[CreationUserId] AS VARCHAR) LIKE N'%B%'
                )
              OR CAST([mtech].[ReportIndex].[UserId] AS VARCHAR) LIKE N'%B%'
              )
            OR CAST([mtech].[ReportIndex].[Id] AS VARCHAR) LIKE N'%B%'
            )
          OR CAST([mtech].[ReportIndex].[LayoutPositionMode] AS VARCHAR) LIKE N'%B%'
          )
        OR CAST([mtech].[ReportIndex].[MtSysLanguagesIRN] AS VARCHAR) LIKE N'%B%'
        )
      OR CAST([mtech].[ReportIndex].[PrintTrackableFlag] AS VARCHAR) LIKE N'%B%'
      )
    OR CAST([mtech].[ReportIndex].[GroupRelationPrefix] AS VARCHAR) LIKE N'%B%'
    )

Yes, I know it's a mess. There's not very much I can do about that.

This query errors out. I get the following error:

ActiveRecord::StatementInvalid (TinyTds::Error: Insufficient result space to convert uniqueidentifier value to char. .... 

I can put this query into mssql-cli and get the same result. HOWEVER, if I change the CAST statement on the uniqueidentifier fields from AS VARCHAR to AS VARCHAR(48) then the statement is successful.

So I have two questions:

  1. Is this working as expected?
  2. What is the right way to initialize this adapter so that uniqueidentifier fields get cast as VARCHAR(48) fields?

I am using ActiveRecord and adapter version 5.1.6. My SQL server version is:

Microsoft SQL Server 2012 (SP1) - 11.0.3128.0 (X64) 
        Dec 28 2012 20:23:12 
        Copyright (c) Microsoft Corporation
        Standard Edition (64-bit) on Windows NT 6.1  (Build 7601: Service Pack 1) (Hypervisor)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions