Skip to content
This repository
Browse code

Fixed that fixtures were being deleted in the same order as inserts c…

…ausing FK errors #890 [andrew.john.peters@gmail.com]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1205 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 03097d3a6047e269641a14f4c93838598011f0b5 1 parent b8c07c0
David Heinemeier Hansson authored April 18, 2005
2  activerecord/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *SVN*
2 2
 
  3
+* Fixed that fixtures were being deleted in the same order as inserts causing FK errors #890 [andrew.john.peters@gmail.com]
  4
+
3 5
 * Fixed loading of fixtures in to be in the right order (or PostgreSQL would bark) #1047 [stephenh@chase3000.com]
4 6
 
5 7
 * Fixed page caching for non-vhost applications living underneath the root #1004 [Ben Schumacher]
21  activerecord/lib/active_record/fixtures.rb
@@ -251,6 +251,8 @@ def self.reset_sequences(connection, table_names)
251 251
     end
252 252
   end
253 253
 
  254
+  attr_reader :table_name
  255
+
254 256
   def initialize(connection, table_name, fixture_path, file_filter = DEFAULT_FILTER_RE)
255 257
     @connection, @table_name, @fixture_path, @file_filter = connection, table_name, fixture_path, file_filter
256 258
     @class_name = Inflector.classify(@table_name)
@@ -470,30 +472,35 @@ def self.method_added(method)
470 472
       private
471 473
         def load_fixtures
472 474
           @loaded_fixtures = {}
473  
-          fixture_table_names.each do |table_name|
474  
-            @loaded_fixtures[table_name] = Fixtures.create_fixtures(fixture_path, table_name)
  475
+          fixtures = Fixtures.create_fixtures(fixture_path, fixture_table_names)
  476
+          unless fixtures.nil?
  477
+            if fixtures.instance_of?(Fixtures)
  478
+              @loaded_fixtures[fixtures.table_name] = fixtures
  479
+            else
  480
+              fixtures.each { |f| @loaded_fixtures[f.table_name] = f }
  481
+            end
475 482
           end
476 483
         end
477  
-        
  484
+
478 485
         # for pre_loaded_fixtures, only require the classes once. huge speed improvement
479 486
         @@required_fixture_classes = false
480  
-        
  487
+
481 488
         def instantiate_fixtures
482 489
           if pre_loaded_fixtures
483 490
             raise RuntimeError, 'Load fixtures before instantiating them.' if Fixtures.all_loaded_fixtures.empty?
484 491
             unless @@required_fixture_classes
485  
-              self.class.require_fixture_classes Fixtures.all_loaded_fixtures.keys 
  492
+              self.class.require_fixture_classes Fixtures.all_loaded_fixtures.keys
486 493
               @@required_fixture_classes = true
487 494
             end
488 495
             Fixtures.instantiate_all_loaded_fixtures(self, load_instances?)
489  
-          else 
  496
+          else
490 497
             raise RuntimeError, 'Load fixtures before instantiating them.' if @loaded_fixtures.nil?
491 498
             @loaded_fixtures.each do |table_name, fixtures|
492 499
               Fixtures.instantiate_fixtures(self, table_name, fixtures, load_instances?)
493 500
             end
494 501
           end
495 502
         end
496  
-        
  503
+
497 504
         def load_instances?
498 505
           use_instantiated_fixtures != :no_instances
499 506
         end
11  activerecord/test/fixtures/db_definitions/db2.sql
@@ -164,3 +164,14 @@ CREATE TABLE categories_posts (
164 164
   category_id int NOT NULL,
165 165
   post_id int NOT NULL
166 166
 );
  167
+
  168
+CREATE TABLE fk_test_has_pk (
  169
+  id INTEGER NOT NULL PRIMARY KEY
  170
+);
  171
+
  172
+CREATE TABLE fk_test_has_fk (
  173
+  id    INTEGER NOT NULL PRIMARY KEY,
  174
+  fk_id INTEGER NOT NULL,
  175
+
  176
+  FOREIGN KEY (fk_id) REFERENCES fk_test_has_pk(id)
  177
+);
11  activerecord/test/fixtures/db_definitions/mysql.sql
@@ -166,3 +166,14 @@ CREATE TABLE `categories_posts` (
166 166
   `category_id` int(11) NOT NULL,
167 167
   `post_id` int(11) NOT NULL
168 168
 );
  169
+
  170
+CREATE TABLE `fk_test_has_pk` (
  171
+  `id` INTEGER NOT NULL PRIMARY KEY
  172
+);
  173
+
  174
+CREATE TABLE `fk_test_has_fk` (
  175
+  `id`    INTEGER NOT NULL PRIMARY KEY,
  176
+  `fk_id` INTEGER NOT NULL,
  177
+
  178
+  FOREIGN KEY (`fk_id`) REFERENCES `fk_test_has_pk`(`id`)
  179
+);
9  activerecord/test/fixtures/db_definitions/oci.sql
@@ -202,3 +202,12 @@ create table categories_posts (
202 202
   category_id integer not null references developers initially deferred disable,
203 203
   post_id int integer not null references developers initially deferred disable
204 204
 );
  205
+
  206
+create table fk_test_has_pk (
  207
+  id integer not null primary key
  208
+);
  209
+
  210
+create table fk_test_has_fk (
  211
+  id integer not null primary key,
  212
+  fk_id integer not null references fk_test_has_fk initially deferred disable,
  213
+);
9  activerecord/test/fixtures/db_definitions/postgresql.sql
@@ -183,3 +183,12 @@ CREATE TABLE categories_posts (
183 183
   category_id integer NOT NULL,
184 184
   post_id integer NOT NULL
185 185
 );
  186
+
  187
+CREATE TABLE fk_test_has_pk (
  188
+  id INTEGER NOT NULL PRIMARY KEY
  189
+);
  190
+
  191
+CREATE TABLE fk_test_has_fk (
  192
+  id    INTEGER NOT NULL PRIMARY KEY,
  193
+  fk_id INTEGER NOT NULL REFERENCES fk_test_has_fk(id)
  194
+);
11  activerecord/test/fixtures/db_definitions/sqlite.sql
@@ -151,3 +151,14 @@ CREATE TABLE 'categories_posts' (
151 151
   'category_id' INTEGER NOT NULL,
152 152
   'post_id' INTEGER NOT NULL
153 153
 );
  154
+
  155
+CREATE TABLE 'fk_test_has_pk' (
  156
+  'id' INTEGER NOT NULL PRIMARY KEY
  157
+);
  158
+
  159
+CREATE TABLE 'fk_test_has_fk' (
  160
+  'id'    INTEGER NOT NULL PRIMARY KEY,
  161
+  'fk_id' INTEGER NOT NULL,
  162
+
  163
+  FOREIGN KEY ('fk_id') REFERENCES 'fk_test_has_pk'('id')
  164
+);
11  activerecord/test/fixtures/db_definitions/sqlserver.sql
@@ -151,3 +151,14 @@ CREATE TABLE categories_posts (
151 151
   category_id int NOT NULL,
152 152
   post_id int NOT NULL
153 153
 );
  154
+
  155
+CREATE TABLE fk_test_has_pk (
  156
+  id INTEGER NOT NULL PRIMARY KEY
  157
+);
  158
+
  159
+CREATE TABLE fk_test_has_fk (
  160
+  id    INTEGER NOT NULL PRIMARY KEY,
  161
+  fk_id INTEGER NOT NULL,
  162
+
  163
+  FOREIGN KEY (fk_id) REFERENCES fk_test_has_pk(id)
  164
+);
3  activerecord/test/fixtures/fk_test_has_fk.yml
... ...
@@ -0,0 +1,3 @@
  1
+first:
  2
+  id: 1
  3
+  fk_id: 1
2  activerecord/test/fixtures/fk_test_has_pk.yml
... ...
@@ -0,0 +1,2 @@
  1
+first:
  2
+  id: 1
24  activerecord/test/fixtures_test.rb
@@ -179,3 +179,27 @@ def test_fixture_table_names
179 179
     assert_equal([:topics, :developers, :accounts], fixture_table_names)
180 180
   end
181 181
 end
  182
+
  183
+
  184
+class ForeignKeyFixturesTest < Test::Unit::TestCase
  185
+  fixtures :fk_test_has_pk, :fk_test_has_fk
  186
+
  187
+  # if foreign keys are implemented and fixtures
  188
+  # are not deleted in reverse order then this test
  189
+  # case will raise StatementInvalid
  190
+
  191
+  def test_number1
  192
+    assert true
  193
+  end
  194
+
  195
+  def test_number2
  196
+    assert true
  197
+  end
  198
+
  199
+end
  200
+
  201
+
  202
+
  203
+
  204
+
  205
+

0 notes on commit 03097d3

Please sign in to comment.
Something went wrong with that request. Please try again.