保存后复合主键不更新

时间:2017-04-09 02:53:08

标签: ruby-on-rails rails-activerecord composite-primary-key

这是一个最小的测试用例,构成了我的问题的基础。为什么即使user已正确保存,属性user.id也未更新?尝试重新查找数据库中的记录会毫无问题地获取该记录,并且id属性已正确设置。

AFAICT,这不是尝试在sqlite中自动增加复合主键的问题。同样的问题也出现在uuid / PostgreSQL组合中。架构只有id作为主键,[ :account_id, :id ]是一个独立的唯一索引。

#!/usr/bin/env ruby
gem "rails", "~> 5.0.2"
gem "composite_primary_keys"

require "active_record"
require "composite_primary_keys"

ActiveRecord::Base.establish_connection(
  adapter: "sqlite3",
  database: ":memory:"
)

ActiveRecord::Schema.define do
  create_table :accounts, force: true do |t|
  end

  create_table :users, force: true do |t|
    t.references :account
    t.index [ :account_id, :id ], unique: true
  end
end

class User < ActiveRecord::Base
  self.primary_keys = [ :account_id, :id ]
  belongs_to :account, inverse_of: :users
end

class Account < ActiveRecord::Base
  has_many :users, inverse_of: :account
end

account = Account.create!
puts "created account: #{account.inspect}"
user = account.users.build
puts "before user.save: #{user.inspect}"
user.save
puts "after user.save: #{user.inspect}"
puts "account.users.first: #{account.users.first.inspect}"

运行该脚本的结果是:

~/src
frankjmattia@lappy-i686(ttys005)[4146] % ./cpk-test.rb
-- create_table(:accounts, {:force=>true})
   -> 0.0036s
-- create_table(:users, {:force=>true})
   -> 0.0009s
created account: #<Account id: 1>
before user.save: #<User id: nil, account_id: 1>
after user.save: #<User id: nil, account_id: 1>
account.users.first: #<User id: 1, account_id: 1>

第一次保存后,user.id不应该是[1,1]吗?如果这是一个错误,我应该向谁报告?

2 个答案:

答案 0 :(得分:1)

事实证明,答案很简单。 Rails通常从创建中获取返回的主键,并使用它更新模型。复合键不会自行重载,所以我必须这样做。基本上在after_create挂钩中使用来自reload的逻辑来获取创建的记录并相应地更新属性。

#!/usr/bin/env ruby
gem "rails", "5.0.2"
gem "composite_primary_keys", "9.0.6"

require "active_record"
require "composite_primary_keys"

ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Schema.define do
  create_table :accounts, force: true    
  create_table :users, force: true do |t|
    t.integer :account_id, null: false
    t.string :email, null: false
    t.index [ :account_id, :id ], unique: true
    t.index [ :account_id, :email ], unique: true
  end
end

class User < ActiveRecord::Base
  self.primary_keys = [ :account_id, :id ]
  belongs_to :account, inverse_of: :users

  after_create do
    self.class.connection.clear_query_cache
    fresh_person = self.class.unscoped {
      self.class.find_by!(account: account, email: email)
    }
    @attributes = fresh_person.instance_variable_get('@attributes')
    @new_record = false
    self
  end
end

class Account < ActiveRecord::Base
  has_many :users, inverse_of: :account
end

account = Account.create!
user = account.users.build(email: "#{SecureRandom.hex(4)}@example.com")
puts "before save user: #{user.inspect}"
user.save
puts "after save user: #{user.inspect}"

现在:

frankjmattia@lappy-i686(ttys003)[4108] % ./cpk-test.rb
-- create_table(:accounts, {:force=>true})
   -> 0.0045s
-- create_table(:users, {:force=>true})
   -> 0.0009s
before save user: #<User id: nil, account_id: 1, email: "a54c2385@example.com">
after save user: #<User id: 1, account_id: 1, email: "a54c2385@example.com">

答案 1 :(得分:0)

SQLite不支持复合主键的自动增量。您可以在SO中找到相关问题:12

这是来自第二个链接的@SingleNegationElimination答案:

  

在sqlite中,只有一个整数时才会获得自动增量行为   列是主键。复合键可防止自动增量   生效。

     

您可以通过将id定义为唯一的主键来获得类似的结果,   但随后在id,col3上添加了一个额外的唯一约束。

composite_primary_keys保持这种逻辑。

此处还有诀窍:sqlite: multi-column primary key with an auto increment column