【发布时间】:2012-02-25 23:56:22
【问题描述】:
**Software Versions:**
Rails 3.1.3
MySQL 5.5.21
OS: MacOS 10.7.3
今天我在我的 MySQL 数据库中添加了很多外键约束。 但现在我在简单插入时遇到“锁定等待超时”异常:
ActiveRecord::StatementInvalid (Mysql2::Error: Lock wait timeout exceeded;
try restarting transaction
如果我从我的数据库中删除外键约束,问题就消失了。
当我尝试为“has_one”关联的“accepts_nested_attributes_for”添加一个包含数据的对象时会出现问题:
class MyApp::PrimaryData < ActiveRecord::Base
has_one :sub_data, :dependent => :destroy
accepts_nested_attributes_for :sub_data
[...]
end
class MyApp::SubData < ActiveRecord::Base
belongs_to :primary_data
attr_accessible :field1, :field2
[...]
end
table: primary_data
-------------------
- id (integer)
- field1 (string)
[...]
table: sub_data
---------------
- id (integer)
- primary_data_id (integer)
- field1 (string)
- field2 (string)
[...]
-> foreign_key_contraint on primary_data_id --> table primary_datas (id)
如果我只创建没有“SubData”的“PrimaryData”,或者分别创建“PrimaryData”和“SubData”,那么我不会收到 MySQL 错误,只有当我尝试使用一些“SubData”创建“PrimaryData”时" 通过 Rails 的“accepts_nested_attributes_for”方式。
有人可以帮我解决这个问题吗? 提前致谢。
【问题讨论】:
-
我想我以前见过这个问题,但我不太记得在什么情况下;和你描述的不一样。我似乎记得我通过简单地重新启动服务器和 MySql 解决了我的问题。不过我可能错了……
-
您使用哪个引擎? InnoDB 还是 MyISAM?当应用程序崩溃时,您可以发布您的日志示例吗?当然,如果还不算太晚:)
-
@basgys 这是什么问题? MyISAM 不支持外键检查。因此我很确定它是 InnoDB。
标签: mysql ruby-on-rails activerecord ruby-on-rails-3.1