rails-sqlserver / tiny_tds

TinyTDS - Simple and fast FreeTDS bindings for Ruby using DB-Library.
Other
607 stars 189 forks source link

Binary column type is being treated as a String type #476

Closed jeremysenn closed 4 years ago

jeremysenn commented 4 years ago

Environment

Operating System

ProductName: Mac OS X ProductVersion: 10.15.5 BuildVersion: 19F101

Rails version: 6.0.3 SQL Server adapter version: 6.0.0 SQL Server: 2012

TinyTDS Version and Information

TinyTDS version: 2.1.2

Version: freetds v1.00.21
freetds.conf directory: /usr/local/Cellar/freetds/1.00.21/etc
MS db-lib source compatibility: no
Sybase binary compatibility: no
Thread safety: yes
iconv library: yes
TDS version: 7.3
iODBC: no
unixodbc: no
SSPI "trusted" logins: no
Kerberos: no
OpenSSL: yes
GnuTLS: no
MARS: no

FreeTDS Version

freetds v1.00.21

Description

A table's image/binary type column is being treated as a string type column, which results in the following error when saving blob image data to that column:

ActiveRecord::StatementInvalid: ArgumentError: string contains null byte

The same exact image/binary column is correctly shown as a binary column in earlier versions (SQL Server adapter version 4.1.4 and TinyTDS version 1.0.5), allowing for successful saving of blob image data to the column.

jeremysenn commented 4 years ago

Turns out this issue was similar to this one:

https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/issues/643

I created a custom attribute in an initializer (rails_sqlserver_types.rb):

ActiveRecord::Type.register(:var_binary_max, ActiveRecord::Type::SQLServer::VarbinaryMax)

And then applied the custom attribute in the model:

attribute :jpeg_image, :var_binary_max