[rspec-users] Foreign key constraints, fixtures, and rake task

Jay Levitt lists-rspec at shopwatch.org
Mon Aug 27 12:11:24 EDT 2007

David Chelimsky wrote:
> On 8/27/07, Jay Levitt <lists-rspec at shopwatch.org> wrote:
>> Tilmann Singer wrote:
>>> * Jay Levitt <lists-rspec at shopwatch.org> [20070827 03:51]:
>>>> What's rake doing differently?
>>> rake spec isn't doing anything fundamentally different, but the order
>>> the specs are run is propably randomly different from when you run
>>> them with spec so they happen to pass in one case and fail in the
>>> other.
>> Nope, it's definitely not random - script/spec works every time, rake
>> fails every time.
> By default, the spec.opts file, which is loaded w/ the rake task, but
> not implicitly with the script/spec command, uses --reverse, so the
> files are run in reverse order. That would support Tilmann's theory
> combined with your observation that they consistently pass one way and
> fail the other.


# lib/tasks/jaytest.rake
rspec_base = File.expand_path(File.dirname(__FILE__) + 
$LOAD_PATH.unshift(rspec_base) if File.exist?(rspec_base)
require 'spec/rake/spectask'
require 'spec/translator'

desc "Run my bad spec"
Spec::Rake::SpecTask.new(:jaytest) do |t|
   t.spec_files = FileList['spec/models/jaytest_spec.rb']

# spec/models/jaytest_spec.rb
require File.dirname(__FILE__) + '/../spec_helper'

describe User, "in fixture :quentin" do
   fixtures :users

   it "should" do
     @user = users(:quentin)


# mysql>show create table accounts
CONSTRAINT `accounts_opener_id_fk` FOREIGN KEY (`opener_id`) REFERENCES 
`users` (`id`)

# 'rake jaytest' output


ActiveRecord::StatementInvalid in 'User in fixture :quentin should'
Mysql::Error: Cannot delete or update a parent row: a foreign key 
constraint fails (`mediajoint_development/accounts`, CONSTRAINT 
`accounts_opener_id_fk` FOREIGN KEY (`opener_id`) REFERENCES `users` 
(`id`)): DELETE FROM users

Finished in 0.031189 seconds

1 example, 1 failure
rake aborted!
Command ruby 
"spec/models/jaytest_spec.rb"  failed

# ruby 
"spec/models/jaytest_spec.rb" output

More information about the rspec-users mailing list