后的主键,我不是很确定问题所在,但是当试图创建一个(通用)玩笑(模型)我得到以下错误:的Rails似乎没有随机部署
ActiveRecord::RecordNotUnique (Mysql2::Error: Duplicate entry '2147483647' for key 'PRIMARY': INSERT INTO `jokes` (`content`, `created_at`, `id`, `rating`, `updated_at`) VALUES ('dsfgdsfgdfgd', '2013-02-27 16:33:12', 90650754896700, 0, '2013-02-27 16:33:12')):
app/controllers/jokes_controller.rb:141:in `create'
app/controllers/jokes_controller.rb:140:in `create'
当我试图挽救另一个问题:
ActiveRecord::RecordNotUnique (Mysql2::Error: Duplicate entry '2147483647' for key 'PRIMARY': INSERT INTO `jokes` (`content`, `created_at`, `id`, `rating`, `updated_at`) VALUES ('dsfgdsfg', '2013-02-27 16:32:23', 29733688655250, 0, '2013-02-27 16:32:23')):
app/controllers/jokes_controller.rb:141:in `create'
app/controllers/jokes_controller.rb:140:in `create'
的错误中提到,我有一个重复的条目(主键2147483647)。我只能创造一个笑话。在部署之前,我从来没有遇到过这个问题,我能想到的唯一值得注意的事情是数据库类型,从sqlite3到mysql2。
这里是重要的码位从我的笑话型号:
before_create :randomize_id
#...
validates :content, :presence => true
validates :content, :uniqueness => true
#...
private
def randomize_id
begin
self.id = SecureRandom.random_number(100_000_000_000_000)
end while Joke.where(:id => self.id).exists?
end
你为什么要这么做? – 2013-02-27 16:51:59