Permalink
Browse files

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
  • Loading branch information...
1 parent 4db718e commit 638505b90f593c8ee48a060f378ed6a9c93442e1 @NZKoz NZKoz committed Oct 6, 2007
View
19 activerecord/lib/active_record/base.rb
@@ -1968,9 +1968,11 @@ def create_or_update
# 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 @@ def create
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
View
4 activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb
@@ -139,6 +139,10 @@ def insert_fixture(fixture, table_name)
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.
View
3 activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
@@ -247,6 +247,9 @@ def rename_column(table_name, column_name, new_column_name) #:nodoc:
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:
View
17 activerecord/test/base_test.rb
@@ -11,6 +11,7 @@
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 @@ def test_save_nil_string_attributes
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 @@ def test_update_columns_not_equal_attributes
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
View
1 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;
View
4 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
+);
View
4 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;
+DROP GENERATOR mixed_case_monkeys_seq;
+DROP GENERATOR minimalistics_seq;
View
6 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;
View
1 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;
View
5 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");
View
1 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;
View
4 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
+);
View
7 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
View
2 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;
View
5 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;
View
4 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
+);
View
1 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;
View
4 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
+);
View
1 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;
View
4 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
+);
View
1 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
View
4 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

0 comments on commit 638505b

Please sign in to comment.