提交 3043d45e 编写于 作者: A Aaron Patterson

Merge pull request #10455 from patricksrobertson/bigserial_id_not_identifying_pk

Add PK constraint on bigserial ID columns on postgres adapter
...@@ -359,8 +359,12 @@ class TableDefinition < ActiveRecord::ConnectionAdapters::TableDefinition ...@@ -359,8 +359,12 @@ class TableDefinition < ActiveRecord::ConnectionAdapters::TableDefinition
# a record (as primary keys cannot be +nil+). This might be done via the # a record (as primary keys cannot be +nil+). This might be done via the
# +SecureRandom.uuid+ method and a +before_save+ callback, for instance. # +SecureRandom.uuid+ method and a +before_save+ callback, for instance.
def primary_key(name, type = :primary_key, options = {}) def primary_key(name, type = :primary_key, options = {})
return super unless type == :uuid return super unless type = :primary_key
options[:default] = options.fetch(:default, 'uuid_generate_v4()')
if type == :uuid
options[:default] = options.fetch(:default, 'uuid_generate_v4()')
end
options[:primary_key] = true options[:primary_key] = true
column name, type, options column name, type, options
end end
......
...@@ -216,3 +216,32 @@ def test_primaery_key_method_with_ansi_quotes ...@@ -216,3 +216,32 @@ def test_primaery_key_method_with_ansi_quotes
end end
end end
if current_adapter?(:PostgreSQLAdapter)
class PrimaryKeyBigSerialTest < ActiveRecord::TestCase
self.use_transactional_fixtures = false
class Widget < ActiveRecord::Base
end
def setup
@con = ActiveRecord::Base.connection
ActiveRecord::Schema.define do
create_table :widgets, id: :bigserial do |t|
end
end
end
def teardown
ActiveRecord::Schema.define do
drop_table :widgets
end
end
def test_bigserial_primary_key
widget = Widget.create!
assert_not_nil widget.id
end
end
end
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册