diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index b9782fcca0d39ba1da2d4cec072dd8d7d8572c0b..edd0517c6ebf902eb29fb494f4602f2eb49133d7 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,7 @@ +* Allow bigint with default nil for avoiding auto increment primary key. + + *Ryuta Kamizono* + * Remove `DEFAULT_CHARSET` and `DEFAULT_COLLATION` in `MySQLDatabaseTasks`. We should omit the collation entirely rather than providing a default. diff --git a/activerecord/lib/active_record/connection_adapters/mysql/schema_definitions.rb b/activerecord/lib/active_record/connection_adapters/mysql/schema_definitions.rb index 29e8c73d463aa0adefdd644ee450d08545318581..ca7dfda80d35b1a8b8a05c4c97bb037e12f11fdb 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql/schema_definitions.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql/schema_definitions.rb @@ -3,7 +3,7 @@ module ConnectionAdapters module MySQL module ColumnMethods def primary_key(name, type = :primary_key, **options) - options[:auto_increment] = true if type == :bigint + options[:auto_increment] = true if type == :bigint && !options.key?(:default) super end diff --git a/activerecord/lib/active_record/connection_adapters/mysql/schema_dumper.rb b/activerecord/lib/active_record/connection_adapters/mysql/schema_dumper.rb index 3c48d0554ea8b560c5a85ccf65503532ba3f5e41..9dee3172f4d462949c560786eda4ec28488050bf 100644 --- a/activerecord/lib/active_record/connection_adapters/mysql/schema_dumper.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql/schema_dumper.rb @@ -4,8 +4,11 @@ module MySQL module ColumnDumper def column_spec_for_primary_key(column) spec = {} - if column.auto_increment? - spec[:id] = ':bigint' if column.bigint? + if column.bigint? + spec[:id] = ':bigint' + spec[:default] = schema_default(column) || 'nil' unless column.auto_increment? + spec[:unsigned] = 'true' if column.unsigned? + elsif column.auto_increment? spec[:unsigned] = 'true' if column.unsigned? return if spec.empty? else diff --git a/activerecord/test/cases/primary_keys_test.rb b/activerecord/test/cases/primary_keys_test.rb index 5e4ba479883a0cc8276c192078e6634ae25f63ec..344822c88381b092b9af20a1475de775b0aa617f 100644 --- a/activerecord/test/cases/primary_keys_test.rb +++ b/activerecord/test/cases/primary_keys_test.rb @@ -280,6 +280,32 @@ def test_primary_key_method_with_ansi_quotes con.reconnect! end end + + class PrimaryKeyBigintNilDefaultTest < ActiveRecord::TestCase + include SchemaDumpingHelper + + self.use_transactional_tests = false + + def setup + @connection = ActiveRecord::Base.connection + @connection.create_table(:bigint_defaults, id: :bigint, default: nil, force: true) + end + + def teardown + @connection.drop_table :bigint_defaults, if_exists: true + end + + test "primary key with bigint allows default override via nil" do + column = @connection.columns(:bigint_defaults).find { |c| c.name == 'id' } + assert column.bigint? + assert_not column.auto_increment? + end + + test "schema dump primary key with bigint default nil" do + schema = dump_table_schema "bigint_defaults" + assert_match %r{create_table "bigint_defaults", id: :bigint, default: nil}, schema + end + end end if current_adapter?(:PostgreSQLAdapter, :MysqlAdapter, :Mysql2Adapter)