mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
parent
575212a1ba
commit
974e860172
3 changed files with 26 additions and 12 deletions
|
@ -44,8 +44,12 @@ module ActiveRecord
|
|||
def primary_key(name, type = :primary_key, **options)
|
||||
if type == :uuid
|
||||
options[:default] = options.fetch(:default, "gen_random_uuid()")
|
||||
elsif options.delete(:auto_increment) == true && type == :integer
|
||||
type = :serial
|
||||
elsif options.delete(:auto_increment) == true && %i(integer bigint).include?(type)
|
||||
type = if type == :bigint || options[:limit] == 8
|
||||
:bigserial
|
||||
else
|
||||
:serial
|
||||
end
|
||||
end
|
||||
|
||||
super
|
||||
|
|
|
@ -3,7 +3,7 @@ module ActiveRecord
|
|||
module SQLite3
|
||||
module ColumnMethods
|
||||
def primary_key(name, type = :primary_key, **options)
|
||||
if options.delete(:auto_increment) == true && type == :integer
|
||||
if options.delete(:auto_increment) == true && %i(integer bigint).include?(type)
|
||||
type = :primary_key
|
||||
end
|
||||
|
||||
|
|
|
@ -224,7 +224,6 @@ class PrimaryKeyWithAutoIncrementTest < ActiveRecord::TestCase
|
|||
|
||||
def setup
|
||||
@connection = ActiveRecord::Base.connection
|
||||
@connection.create_table(:auto_increments, id: :integer, auto_increment: true, force: true)
|
||||
end
|
||||
|
||||
def teardown
|
||||
|
@ -232,6 +231,17 @@ class PrimaryKeyWithAutoIncrementTest < ActiveRecord::TestCase
|
|||
end
|
||||
|
||||
def test_primary_key_with_auto_increment
|
||||
@connection.create_table(:auto_increments, id: :integer, auto_increment: true, force: true)
|
||||
assert_auto_incremented
|
||||
end
|
||||
|
||||
def test_primary_key_with_auto_increment_and_bigint
|
||||
@connection.create_table(:auto_increments, id: :bigint, auto_increment: true, force: true)
|
||||
assert_auto_incremented
|
||||
end
|
||||
|
||||
private
|
||||
def assert_auto_incremented
|
||||
record1 = AutoIncrement.create!
|
||||
assert_not_nil record1.id
|
||||
|
||||
|
|
Loading…
Reference in a new issue