0

我有一个旧版 Rails(1.2.3 版)应用程序,它在许多服务器上运行都没有问题(更不用说我的本地环境了)。但是,部署到其最新的服务器上,我现在遇到了ActiveRecord::StatementInvalid: Mysql::Error: #23000Column 'video_id' cannot be null错误。

以下是简化的模型/关系:

class Video < ActiveRecord::Base
    has_one(:user, :dependent => :destroy)
end

class User < ActiveRecord::Base
    belongs_to(:video)
end

下面是关系失败的 Rails 控制台记录:

>> video = Video.create(:title => 'New Video')
=> #<Video:0xb6d5e31c>...
>> video.id
=> 5
>> video.user = User.create(:name => 'Tester')
ActiveRecord::StatementInvalid: Mysql::Error: #23000Column 'video_id' cannot be null: INSERT INTO users (`name`, `video_id`) VALUES('Tester', NULL)
    from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/abstract_adapter.rb:128:in `log'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/mysql_adapter.rb:243:in `execute'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/mysql_adapter.rb:253:in `insert'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1811:in `create_without_callbacks'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/callbacks.rb:254:in `create_without_timestamps'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/timestamp.rb:39:in `create'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1789:in `create_or_update_without_callbacks'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/callbacks.rb:242:in `create_or_update'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:1545:in `save_without_validation'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/validations.rb:752:in `save_without_transactions'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/transactions.rb:129:in `save'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/abstract/database_statements.rb:59:in `transaction'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/transactions.rb:95:in `transaction'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/transactions.rb:121:in `transaction'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/transactions.rb:129:in `save'
    from /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/base.rb:451:in `create'
    from (irb):3
    from :0

有没有其他人遇到过 ActiveRecord 在清楚地知道它时不发送 ID 的情况?

4

2 回答 2

2

正确的语法是

video.user.create(:name => 'Tester')

正如你所拥有的,它试图在将其分配给 video.user 之前创建用户,按照 ruby​​ 的评估顺序。

于 2010-04-21T21:18:40.460 回答
0

好吧,我已经将这个成熟的应用程序的这个特定部分迁移到了 rails 2.3.5,现在下面的方法可以解决问题:

video.create_user(:name => 'Tester')

感谢您到目前为止的输入,但如果有人知道为什么 1.2.3 版本的代码可以在它遇到的每台服务器上运行,直到今天(旧的 ActiveRecord 以某种模糊的方式不同意某个版本的 MySQL,也许?)我'我很想听。

于 2010-04-22T01:23:29.913 回答