When using ActiveRecord to store a string into a SqlServer nvchar column, and the length of the string is larger than the maximum length of the column, the string is silently truncated to the column's size. How can I make a warning or error be issued instead of silently truncating the value?
Code that reproduces the issue
Database model:
class Email < ActiveRecord::Base
self.table_name = "Email"
end
The insert that silently truncates:
Email.create!(Address: "X" * 76)
email.reload
p email.Address.size # => 75
The log of the insert statement:
D, [2017-06-30T16:04:35.320283 #9061] DEBUG -- : dest SQL (0.5ms) EXEC sp_executesql N'INSERT INTO [Email] ([Address]) OUTPUT INSERTED.[Id] VALUES (@0)', N'@0 nvarchar(75)', @0 = N'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' [["Address", "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"]]
Schema
CREATE TABLE [dbo].[Email](
[Id] [int] IDENTITY(1,1) NOT NULL,
[Address] [nvarchar](75) NOT NULL,
CONSTRAINT [PK_dbo.Email] PRIMARY KEY CLUSTERED
(
[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Versions
- activerecord (4.2.8)
- activerecord-sqlserver-adapter (4.2.15)
- Ruby 2.4.1
- SqlServer 2014
Additional info
- This is an ActiveRecord project without Rails.