提交 72e59fed 编写于 作者: M Matthew Draper

Tweak bigint PK handling

* Don't force PKs on tables that have explicitly opted out
* All integer-like PKs are autoincrement unless they have an explicit
  default
上级 9f6f51be
......@@ -3,10 +3,8 @@ module ConnectionAdapters
module MySQL
module ColumnMethods
def primary_key(name, type = :primary_key, **options)
if type == :primary_key && !options.key?(:default)
options[:auto_increment] = true
options[:limit] = 8
end
options[:auto_increment] = true if [:primary_key, :integer, :bigint].include?(type) && !options.key?(:default)
options[:limit] = 8 if [:primary_key].include?(type)
super
end
......
......@@ -4,8 +4,8 @@ module MySQL
module ColumnDumper
def column_spec_for_primary_key(column)
spec = super
if column.type == :integer && !column.auto_increment?
spec[:default] = schema_default(column) || "nil"
if [:integer, :bigint].include?(schema_type(column)) && !column.auto_increment?
spec[:default] ||= schema_default(column) || "nil"
end
spec[:unsigned] = "true" if column.unsigned?
spec
......
......@@ -42,6 +42,7 @@ module ColumnMethods
# a record (as primary keys cannot be +nil+). This might be done via the
# +SecureRandom.uuid+ method and a +before_save+ callback, for instance.
def primary_key(name, type = :primary_key, **options)
options[:auto_increment] = true if [:primary_key, :integer, :bigint].include?(type) && !options.key?(:default)
if type == :uuid
options[:default] = options.fetch(:default, "gen_random_uuid()")
elsif options.delete(:auto_increment) == true && %i(integer bigint).include?(type)
......
......@@ -3,7 +3,7 @@ module ConnectionAdapters
module SQLite3
module ColumnMethods
def primary_key(name, type = :primary_key, **options)
if options.delete(:auto_increment) == true && %i(integer bigint).include?(type)
if %i(integer bigint).include?(type) && (options.delete(:auto_increment) == true || !options.key?(:default))
type = :primary_key
end
......
......@@ -24,9 +24,8 @@ def create_table(table_name, options = {})
# Since 5.1 Postgres adapter uses bigserial type for primary
# keys by default and MySQL uses bigint. This compat layer makes old migrations utilize
# serial/int type instead -- the way it used to work before 5.1.
if options[:id].blank?
unless options.key?(:id)
options[:id] = :integer
options[:auto_increment] = true
end
super
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册