2
我使用:
的Rails 3.0.9
MSSQL 2005的Rails 3 ActiveRecord的插入無法插入null值
我有表:
CREATE TABLE [dbo].[edocs](
[id] [int] IDENTITY(1,1) NOT NULL,
[id_claim] [int] NOT NULL,
[id_material] [smallint] NOT NULL,
[is_secondary] [bit] NOT NULL CONSTRAINT [DF_edocs_is_secondary] DEFAULT ((0)),
[title] [varchar](100) COLLATE Ukrainian_CI_AS NOT NULL,
[ext] [varchar](4) COLLATE Ukrainian_CI_AS NOT NULL,
[size] [int] NOT NULL,
[code] [varchar](10) COLLATE Ukrainian_CI_AS NULL,
[receive_date] [datetime] NOT NULL CONSTRAINT [DF_edocs_receive_date] DEFAULT (getdate()),
[reg_date] [datetime] NULL,
[reg_numb] [varchar](10) COLLATE Ukrainian_CI_AS NULL,
[idcead] [int] NULL,
[efile] [int] NULL
)
某些字段有默認值(適用於receive_date
)。
在Rails控制器我嘗試創造新的紀錄:
Edoc.create(
:id_claim => @claim_index,
:id_material => @doc_code,
:title => @file_list.first[:name],
:ext => @file_list.first[:ext],
:size => @file_list.first[:size],
:code => @materials[@doc_code]["code"]
)
但我收到錯誤消息:
ActiveRecord::StatementInvalid (TinyTds::Error: Cannot insert the value NULL int
o column 'receive_date', table 'eFilling.dbo.edocs'; column does not allow nulls
. INSERT fails.: INSERT INTO [edocs] ([id_claim], [id_material], [is_secondary],
[title], [ext], [size], [code], [receive_date], [reg_date], [reg_numb], [idCEAD
], [eFile]) VALUES (100000, 3, 0, N'text', N'rtf', 80
472, N'al', NULL, NULL, NULL, NULL, NULL)):
但在MSSQL 2005控制檯我能做到這一點:
insert into edocs ([id_claim], [id_material],[title], [ext], [size]) values(1, 1, 'rrr', 'rtf', 123)
我不希望ActiveRecord自動通過添加字段來完成查詢,我的未創建方法。
我該怎麼做?
謝謝你的建議空。但是,MSSQL 2005允許我將** insert into edocs([id_claim],[id_material],[title],[ext],[size])values(1,1,'rrr','rtf',123)* *正如你可以看到MSSQL允許我不指定在查詢中具有默認值的字段。 MSSQL自動插入默認值(getdate()) –
那麼爲什麼不把當前的時間戳設置爲默認值呢? –
我該怎麼做? ** Edoc.create(:receive_date => Time.now)**? –