diff options
author | Michael Koziarski <michael@koziarski.com> | 2007-10-06 00:49:58 +0000 |
---|---|---|
committer | Michael Koziarski <michael@koziarski.com> | 2007-10-06 00:49:58 +0000 |
commit | 638505b90f593c8ee48a060f378ed6a9c93442e1 (patch) | |
tree | 6a4e7ecf398429d16ec58e515255f2f43fa6baa4 /activerecord | |
parent | 4db718e2bb514be7a2e76e56cb5027c4007528b4 (diff) | |
download | rails-638505b90f593c8ee48a060f378ed6a9c93442e1.tar.gz rails-638505b90f593c8ee48a060f378ed6a9c93442e1.tar.bz2 rails-638505b90f593c8ee48a060f378ed6a9c93442e1.zip |
Send the correct INSERT statement when dealing with objects with only primary keys. Closes #9523 [tarmo]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7753 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord')
22 files changed, 94 insertions, 8 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 5e1185065e..068d9a5c45 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -1968,9 +1968,11 @@ module ActiveRecord #:nodoc: # Updates the associated record with values matching those of the instance attributes. # Returns the number of affected rows. def update + quoted_attributes = attributes_with_quotes(false, false) + return 0 if quoted_attributes.empty? connection.update( "UPDATE #{self.class.table_name} " + - "SET #{quoted_comma_pair_list(connection, attributes_with_quotes(false, false))} " + + "SET #{quoted_comma_pair_list(connection, quoted_attributes)} " + "WHERE #{connection.quote_column_name(self.class.primary_key)} = #{quote_value(id)}", "#{self.class.name} Update" ) @@ -1983,13 +1985,18 @@ module ActiveRecord #:nodoc: self.id = connection.next_sequence_value(self.class.sequence_name) end - self.id = connection.insert( + quoted_attributes = attributes_with_quotes + + statement = if quoted_attributes.empty? + connection.empty_insert_statement(self.class.table_name) + else "INSERT INTO #{self.class.table_name} " + "(#{quoted_column_names.join(', ')}) " + - "VALUES(#{attributes_with_quotes.values.join(', ')})", - "#{self.class.name} Create", - self.class.primary_key, self.id, self.class.sequence_name - ) + "VALUES(#{quoted_attributes.values.join(', ')})" + end + + self.id = connection.insert(statement, "#{self.class.name} Create", + self.class.primary_key, self.id, self.class.sequence_name) @new_record = false id diff --git a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb index a80025e7a7..341b104f06 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb @@ -139,6 +139,10 @@ module ActiveRecord execute "INSERT INTO #{table_name} (#{fixture.key_list}) VALUES (#{fixture.value_list})", 'Fixture Insert' end + def empty_insert_statement(table_name) + "INSERT INTO #{table_name} VALUES(DEFAULT)" + end + protected # Returns an array of record hashes with the column names as keys and # column values as values. diff --git a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb index 2b78c300f5..c171b0239e 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb @@ -247,6 +247,9 @@ module ActiveRecord alter_table(table_name, :rename => {column_name.to_s => new_column_name.to_s}) end + def empty_insert_statement(table_name) + "INSERT INTO #{table_name} VALUES(NULL)" + end protected def select(sql, name = nil) #:nodoc: diff --git a/activerecord/test/base_test.rb b/activerecord/test/base_test.rb index 4418b8d9e6..53ef4a5e86 100755 --- a/activerecord/test/base_test.rb +++ b/activerecord/test/base_test.rb @@ -11,6 +11,7 @@ require 'fixtures/column_name' require 'fixtures/subscriber' require 'fixtures/keyboard' require 'fixtures/post' +require 'fixtures/minimalistic' class Category < ActiveRecord::Base; end class Smarts < ActiveRecord::Base; end @@ -64,7 +65,7 @@ class TopicWithProtectedContentAndAccessibleAuthorName < ActiveRecord::Base end class BasicsTest < Test::Unit::TestCase - fixtures :topics, :companies, :developers, :projects, :computers, :accounts + fixtures :topics, :companies, :developers, :projects, :computers, :accounts, :minimalistics def test_table_exists assert !NonExistentTable.table_exists? @@ -184,6 +185,15 @@ class BasicsTest < Test::Unit::TestCase assert_nil topic.title end + def test_save_for_record_with_only_primary_key + minimalistic = Minimalistic.new + assert_nothing_raised { minimalistic.save } + end + + def test_save_for_record_with_only_primary_key_that_is_provided + assert_nothing_raised { Minimalistic.create!(:id => 2) } + end + def test_hashes_not_mangled new_topic = { :title => "New Topic" } new_topic_values = { :title => "AnotherTopic" } @@ -240,6 +250,11 @@ class BasicsTest < Test::Unit::TestCase topicReloaded.send :write_attribute, 'does_not_exist', 'test' assert_nothing_raised { topicReloaded.save } end + + def test_update_for_record_with_only_primary_key + minimalistic = minimalistics(:first) + assert_nothing_raised { minimalistic.save } + end def test_write_attribute topic = Topic.new diff --git a/activerecord/test/fixtures/db_definitions/db2.drop.sql b/activerecord/test/fixtures/db_definitions/db2.drop.sql index 9837def303..286066ea20 100644 --- a/activerecord/test/fixtures/db_definitions/db2.drop.sql +++ b/activerecord/test/fixtures/db_definitions/db2.drop.sql @@ -30,3 +30,4 @@ DROP TABLE keyboards; DROP TABLE legacy_things; DROP TABLE numeric_data; DROP TABLE mixed_case_monkeys; +DROP TABLE minimalistics; diff --git a/activerecord/test/fixtures/db_definitions/db2.sql b/activerecord/test/fixtures/db_definitions/db2.sql index 750e74a0c3..6e088bd8ed 100644 --- a/activerecord/test/fixtures/db_definitions/db2.sql +++ b/activerecord/test/fixtures/db_definitions/db2.sql @@ -229,3 +229,7 @@ CREATE TABLE mixed_case_monkeys ( monkeyID INT NOT NULL PRIMARY KEY, fleaCount INT ); + +CREATE TABLE minimalistics ( + id INT NOT NULL PRIMARY KEY +); diff --git a/activerecord/test/fixtures/db_definitions/firebird.drop.sql b/activerecord/test/fixtures/db_definitions/firebird.drop.sql index 69aa093150..378843bf9b 100644 --- a/activerecord/test/fixtures/db_definitions/firebird.drop.sql +++ b/activerecord/test/fixtures/db_definitions/firebird.drop.sql @@ -31,6 +31,7 @@ DROP TABLE defaults; DROP TABLE legacy_things; DROP TABLE numeric_data; DROP TABLE mixed_case_monkeys; +DROP TABLE minimalistics; DROP DOMAIN D_BOOLEAN; @@ -60,4 +61,5 @@ DROP GENERATOR keyboards_seq; DROP GENERATOR defaults_seq; DROP GENERATOR legacy_things_seq; DROP GENERATOR numeric_data_seq; -DROP GENERATOR mixed_case_monkeys_seq;
\ No newline at end of file +DROP GENERATOR mixed_case_monkeys_seq; +DROP GENERATOR minimalistics_seq; diff --git a/activerecord/test/fixtures/db_definitions/firebird.sql b/activerecord/test/fixtures/db_definitions/firebird.sql index 779701c354..9ed6453ff7 100644 --- a/activerecord/test/fixtures/db_definitions/firebird.sql +++ b/activerecord/test/fixtures/db_definitions/firebird.sql @@ -302,3 +302,9 @@ CREATE TABLE mixed_case_monkeys ( ); CREATE GENERATOR mixed_case_monkeys_seq; SET GENERATOR mixed_case_monkeys_seq TO 10000; + +CREATE TABLE minimalistics ( + id BIGINT NOT NULL +); +CREATE GENERATOR minimalistics_seq; +SET GENERATOR minimalistics_seq TO 10000; diff --git a/activerecord/test/fixtures/db_definitions/frontbase.drop.sql b/activerecord/test/fixtures/db_definitions/frontbase.drop.sql index 1c87fdf7ed..58ec56bd4f 100644 --- a/activerecord/test/fixtures/db_definitions/frontbase.drop.sql +++ b/activerecord/test/fixtures/db_definitions/frontbase.drop.sql @@ -30,3 +30,4 @@ DROP TABLE keyboards CASCADE; DROP TABLE legacy_things CASCADE; DROP TABLE numeric_data CASCADE; DROP TABLE mixed_case_monkeys CASCADE; +DROP TABLE minimalistics CASCADE; diff --git a/activerecord/test/fixtures/db_definitions/frontbase.sql b/activerecord/test/fixtures/db_definitions/frontbase.sql index 798b0204d2..83441798b4 100644 --- a/activerecord/test/fixtures/db_definitions/frontbase.sql +++ b/activerecord/test/fixtures/db_definitions/frontbase.sql @@ -266,3 +266,8 @@ CREATE TABLE mixed_case_monkeys ( "fleaCount" integer ); SET UNIQUE FOR mixed_case_monkeys("monkeyID"); + +CREATE TABLE minimalistics ( + "id" integer NOT NULL +); +SET UNIQUE FOR minimalistics("id"); diff --git a/activerecord/test/fixtures/db_definitions/mysql.drop.sql b/activerecord/test/fixtures/db_definitions/mysql.drop.sql index f269b3a3b7..419cdaa52c 100644 --- a/activerecord/test/fixtures/db_definitions/mysql.drop.sql +++ b/activerecord/test/fixtures/db_definitions/mysql.drop.sql @@ -30,3 +30,4 @@ DROP TABLE keyboards; DROP TABLE legacy_things; DROP TABLE numeric_data; DROP TABLE mixed_case_monkeys; +DROP TABLE minimalistics; diff --git a/activerecord/test/fixtures/db_definitions/mysql.sql b/activerecord/test/fixtures/db_definitions/mysql.sql index 1450d20890..a2195a4f5a 100755 --- a/activerecord/test/fixtures/db_definitions/mysql.sql +++ b/activerecord/test/fixtures/db_definitions/mysql.sql @@ -232,3 +232,7 @@ CREATE TABLE mixed_case_monkeys ( `fleaCount` int(11), PRIMARY KEY (`monkeyID`) ) TYPE=InnoDB; + +CREATE TABLE `minimalistics` ( + `id` INTEGER NOT NULL auto_increment PRIMARY KEY +); diff --git a/activerecord/test/fixtures/db_definitions/openbase.sql b/activerecord/test/fixtures/db_definitions/openbase.sql index a177d1688b..ec0029b0f6 100644 --- a/activerecord/test/fixtures/db_definitions/openbase.sql +++ b/activerecord/test/fixtures/db_definitions/openbase.sql @@ -309,3 +309,10 @@ CREATE TABLE mixed_case_monkeys ( go CREATE PRIMARY KEY mixed_case_monkeys (monkeyID) go + +CREATE TABLE minimalistics ( + id INTEGER NOT NULL DEFAULT _rowid +); +go +CREATE PRIMARY KEY minimalistics (id) +go diff --git a/activerecord/test/fixtures/db_definitions/oracle.drop.sql b/activerecord/test/fixtures/db_definitions/oracle.drop.sql index 910b118e05..21f40ed8d0 100644 --- a/activerecord/test/fixtures/db_definitions/oracle.drop.sql +++ b/activerecord/test/fixtures/db_definitions/oracle.drop.sql @@ -31,6 +31,7 @@ drop table keyboards; drop table legacy_things; drop table numeric_data; drop table mixed_case_monkeys; +drop table minimalistics; drop sequence accounts_seq; drop sequence funny_jokes_seq; @@ -63,3 +64,4 @@ drop sequence keyboards_seq; drop sequence legacy_things_seq; drop sequence numeric_data_seq; drop sequence mixed_case_monkeys_seq; +drop sequence minimalistics_seq; diff --git a/activerecord/test/fixtures/db_definitions/oracle.sql b/activerecord/test/fixtures/db_definitions/oracle.sql index f84a86a655..22ca0baa8d 100644 --- a/activerecord/test/fixtures/db_definitions/oracle.sql +++ b/activerecord/test/fixtures/db_definitions/oracle.sql @@ -323,3 +323,8 @@ CREATE TABLE mixed_case_monkeys ( "fleaCount" INTEGER ); create sequence mixed_case_monkeys_seq minvalue 10000; + +CREATE TABLE minimalistics ( + id INTEGER NOT NULL PRIMARY KEY +); +create sequence minimalistics_seq minvalue 10000; diff --git a/activerecord/test/fixtures/db_definitions/postgresql.sql b/activerecord/test/fixtures/db_definitions/postgresql.sql index bbe54599d8..3b929e338c 100644 --- a/activerecord/test/fixtures/db_definitions/postgresql.sql +++ b/activerecord/test/fixtures/db_definitions/postgresql.sql @@ -285,3 +285,7 @@ CREATE TABLE postgresql_oids ( id SERIAL PRIMARY KEY, obj_id OID ); + +CREATE TABLE minimalistics ( + id serial primary key +); diff --git a/activerecord/test/fixtures/db_definitions/sqlite.drop.sql b/activerecord/test/fixtures/db_definitions/sqlite.drop.sql index f269b3a3b7..419cdaa52c 100644 --- a/activerecord/test/fixtures/db_definitions/sqlite.drop.sql +++ b/activerecord/test/fixtures/db_definitions/sqlite.drop.sql @@ -30,3 +30,4 @@ DROP TABLE keyboards; DROP TABLE legacy_things; DROP TABLE numeric_data; DROP TABLE mixed_case_monkeys; +DROP TABLE minimalistics; diff --git a/activerecord/test/fixtures/db_definitions/sqlite.sql b/activerecord/test/fixtures/db_definitions/sqlite.sql index 973639f177..2e19dd2533 100644 --- a/activerecord/test/fixtures/db_definitions/sqlite.sql +++ b/activerecord/test/fixtures/db_definitions/sqlite.sql @@ -213,3 +213,7 @@ CREATE TABLE mixed_case_monkeys ( 'monkeyID' INTEGER NOT NULL PRIMARY KEY, 'fleaCount' INTEGER ); + +CREATE TABLE minimalistics ( + 'id' INTEGER NOT NULL PRIMARY KEY +); diff --git a/activerecord/test/fixtures/db_definitions/sqlserver.drop.sql b/activerecord/test/fixtures/db_definitions/sqlserver.drop.sql index c415d223f5..a886aa3efb 100644 --- a/activerecord/test/fixtures/db_definitions/sqlserver.drop.sql +++ b/activerecord/test/fixtures/db_definitions/sqlserver.drop.sql @@ -32,3 +32,4 @@ DROP TABLE legacy_things; DROP TABLE numeric_data; DROP TABLE [order]; DROP TABLE mixed_case_monkeys; +DROP TABLE minimalistics; diff --git a/activerecord/test/fixtures/db_definitions/sqlserver.sql b/activerecord/test/fixtures/db_definitions/sqlserver.sql index 3cec4e4a16..81b79bfe5c 100644 --- a/activerecord/test/fixtures/db_definitions/sqlserver.sql +++ b/activerecord/test/fixtures/db_definitions/sqlserver.sql @@ -241,3 +241,7 @@ CREATE TABLE mixed_case_monkeys ( [monkeyID] int NOT NULL IDENTITY(1, 1), [fleaCount] int default NULL ); + +CREATE TABLE minimalistics ( + id int NOT NULL IDENTITY(1, 1) PRIMARY KEY +); diff --git a/activerecord/test/fixtures/db_definitions/sybase.drop.sql b/activerecord/test/fixtures/db_definitions/sybase.drop.sql index 9d85221610..ebb91931fb 100644 --- a/activerecord/test/fixtures/db_definitions/sybase.drop.sql +++ b/activerecord/test/fixtures/db_definitions/sybase.drop.sql @@ -30,5 +30,6 @@ DROP TABLE keyboards DROP TABLE legacy_things DROP TABLE numeric_data DROP TABLE mixed_case_monkeys +DROP TABLE minimalistics DROP TABLE schema_info go diff --git a/activerecord/test/fixtures/db_definitions/sybase.sql b/activerecord/test/fixtures/db_definitions/sybase.sql index 6c42c66031..9f0cb3ebe2 100644 --- a/activerecord/test/fixtures/db_definitions/sybase.sql +++ b/activerecord/test/fixtures/db_definitions/sybase.sql @@ -215,4 +215,8 @@ CREATE TABLE mixed_case_monkeys ( [fleaCount] numeric(9,0) ); +CREATE TABLE minimalistics ( + id numeric(9,0) IDENTITY PRIMARY KEY +); + go |