Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
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...
commit 638505b90f593c8ee48a060f378ed6a9c93442e1 1 parent 4db718e
Michael Koziarski NZKoz authored
Showing with 94 additions and 8 deletions.
  1. +13 −6 activerecord/lib/active_record/base.rb
  2. +4 −0 activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb
  3. +3 −0  activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
  4. +16 −1 activerecord/test/base_test.rb
  5. +1 −0  activerecord/test/fixtures/db_definitions/db2.drop.sql
  6. +4 −0 activerecord/test/fixtures/db_definitions/db2.sql
  7. +3 −1 activerecord/test/fixtures/db_definitions/firebird.drop.sql
  8. +6 −0 activerecord/test/fixtures/db_definitions/firebird.sql
  9. +1 −0  activerecord/test/fixtures/db_definitions/frontbase.drop.sql
  10. +5 −0 activerecord/test/fixtures/db_definitions/frontbase.sql
  11. +1 −0  activerecord/test/fixtures/db_definitions/mysql.drop.sql
  12. +4 −0 activerecord/test/fixtures/db_definitions/mysql.sql
  13. +7 −0 activerecord/test/fixtures/db_definitions/openbase.sql
  14. +2 −0  activerecord/test/fixtures/db_definitions/oracle.drop.sql
  15. +5 −0 activerecord/test/fixtures/db_definitions/oracle.sql
  16. +4 −0 activerecord/test/fixtures/db_definitions/postgresql.sql
  17. +1 −0  activerecord/test/fixtures/db_definitions/sqlite.drop.sql
  18. +4 −0 activerecord/test/fixtures/db_definitions/sqlite.sql
  19. +1 −0  activerecord/test/fixtures/db_definitions/sqlserver.drop.sql
  20. +4 −0 activerecord/test/fixtures/db_definitions/sqlserver.sql
  21. +1 −0  activerecord/test/fixtures/db_definitions/sybase.drop.sql
  22. +4 −0 activerecord/test/fixtures/db_definitions/sybase.sql
19 activerecord/lib/active_record/base.rb
View
@@ -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
4 activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb
View
@@ -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.
3  activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
View
@@ -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:
17 activerecord/test/base_test.rb
View
@@ -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
1  activerecord/test/fixtures/db_definitions/db2.drop.sql
View
@@ -30,3 +30,4 @@ DROP TABLE keyboards;
DROP TABLE legacy_things;
DROP TABLE numeric_data;
DROP TABLE mixed_case_monkeys;
+DROP TABLE minimalistics;
4 activerecord/test/fixtures/db_definitions/db2.sql
View
@@ -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
+);
4 activerecord/test/fixtures/db_definitions/firebird.drop.sql
View
@@ -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;
6 activerecord/test/fixtures/db_definitions/firebird.sql
View
@@ -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;
1  activerecord/test/fixtures/db_definitions/frontbase.drop.sql
View
@@ -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;
5 activerecord/test/fixtures/db_definitions/frontbase.sql
View
@@ -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");
1  activerecord/test/fixtures/db_definitions/mysql.drop.sql
View
@@ -30,3 +30,4 @@ DROP TABLE keyboards;
DROP TABLE legacy_things;
DROP TABLE numeric_data;
DROP TABLE mixed_case_monkeys;
+DROP TABLE minimalistics;
4 activerecord/test/fixtures/db_definitions/mysql.sql
View
@@ -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
+);
7 activerecord/test/fixtures/db_definitions/openbase.sql
View
@@ -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
2  activerecord/test/fixtures/db_definitions/oracle.drop.sql
View
@@ -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;
5 activerecord/test/fixtures/db_definitions/oracle.sql
View
@@ -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;
4 activerecord/test/fixtures/db_definitions/postgresql.sql
View
@@ -285,3 +285,7 @@ CREATE TABLE postgresql_oids (
id SERIAL PRIMARY KEY,
obj_id OID
);
+
+CREATE TABLE minimalistics (
+ id serial primary key
+);
1  activerecord/test/fixtures/db_definitions/sqlite.drop.sql
View
@@ -30,3 +30,4 @@ DROP TABLE keyboards;
DROP TABLE legacy_things;
DROP TABLE numeric_data;
DROP TABLE mixed_case_monkeys;
+DROP TABLE minimalistics;
4 activerecord/test/fixtures/db_definitions/sqlite.sql
View
@@ -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
+);
1  activerecord/test/fixtures/db_definitions/sqlserver.drop.sql
View
@@ -32,3 +32,4 @@ DROP TABLE legacy_things;
DROP TABLE numeric_data;
DROP TABLE [order];
DROP TABLE mixed_case_monkeys;
+DROP TABLE minimalistics;
4 activerecord/test/fixtures/db_definitions/sqlserver.sql
View
@@ -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
+);
1  activerecord/test/fixtures/db_definitions/sybase.drop.sql
View
@@ -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
4 activerecord/test/fixtures/db_definitions/sybase.sql
View
@@ -215,4 +215,8 @@ CREATE TABLE mixed_case_monkeys (
[fleaCount] numeric(9,0)
);
+CREATE TABLE minimalistics (
+ id numeric(9,0) IDENTITY PRIMARY KEY
+);
+
go
Please sign in to comment.
Something went wrong with that request. Please try again.