2

在将 rails 应用程序从 rails3.2 升级到 rails 4 时。我遇到了一个问题

ActiveRecord::StatementInvalid 在 SessionsController#new

Mysql::Error: Field 'session_id' doesn't have a default value: INSERT INTO sessions( created_at, data, updated_at) VALUES (?, ?, ?)

在我的会话表中,我有 session_id 字段,但我不知道如何设置默认值。

你能帮我解决这个问题吗?

我的 add_sessions_table.rb 迁移文件:

类 AddSessionsTable < ActiveRecord::Migration

定义变化

create_table :sessions do |t|
  t.string :session_id, :null => false
  t.text :data
  t.timestamps
end

add_index :sessions, :session_id
add_index :sessions, :updated_at    
end

结尾

我的 session_store.rb 文件

MyApp::Application.config.session_store :active_record_store

谢谢

4

1 回答 1

5

将此代码添加到初始化程序中,

ActiveRecord::SessionStore::Session.attr_accessible :data, :session_id

有关更多信息,请查看activerecord-session_store链接

于 2013-10-22T08:31:24.457 回答