SQLException:没有这样的表:main。[]:CREATE INDEX

时间:2014-05-14 06:18:38

标签: ruby-on-rails-4 rails-migrations

我只是使用rake db:reset

重置我的数据库

我的模型文件夹中有3个表:Performer表,Performance表和两个PerformerPerformance之间的关系表。

当我运行db:migrate时,我得到表不存在的错误

-- add_index(:performer_performances, :performer_id)
-- add_index(:performer_performances, :performer_id)
rake aborted!
SQLite3::SQLException: no such table: main.performer_performances: CREATE  INDEX
 "index_performer_performances_on_performer_id" ON "performer_performances" ("pe
rformer_id")
C:/rubyProjects/dads/db/migrate/20140315155732_create_performer_performances.rb:
9:in `<class:CreatePerformerPerformances>'
C:/rubyProjects/dads/db/migrate/20140315155732_create_performer_performances.rb:
1:in `<top (required)>'
C:131071:in `disable_ddl_transaction'
Tasks: TOP => db:migrate
(See full trace by running task with --trace) #ill include output with trace at the end

这是我的CreatePerformerPerformances迁移文件:

class CreatePerformerPerformances < ActiveRecord::Migration
  def change
    create_table :performer_performances do |t|
      t.integer :performer_id
      t.integer :performance_id
      t.timestamps
    end
  end
    add_index :performer_performances, :performer_id
    add_index :performer_performances, :performance_id
    add_index :performer_performances, [:performer_id, :performance_id], unique: true
end

我的PerformerPerformance模型

class PerformerPerformance < ActiveRecord::Base

  belongs_to :performer
  belongs_to :performance
  validates :performer_id, presence: true
  validates :performance_id, presence: true
end

表演者模特

class Performer < ActiveRecord::Base
   validates :name, presence: true, length: { maximum: 50 },  uniqueness: { case_sensitive: false }
   has_many :performer_performances, dependent: :destroy  
   has_many :performances, through: :performer_performances

   def part_of?(performance) 
     performer_performances.find_by(performance_id: performance.id)
   end

   def perform_in!(performance)
      performer_performances.create!(performance_id: performance.id)
   end

end

表现模式:

class Performance < ActiveRecord::Base
   validates :date, length: { maximum: 30 }  
   validates :location, length: { maximum: 60 }
   has_many :performer_performances, dependent: :destroy  
   has_many :performers, through: :performer_performances

   def has_performer?(performer) 
     performer_performances.find_by(performer_id: performer.id)
   end

   def include! (performer)
     performer_performances.create!(performer_id: performer.id)
   end

end

以下是跟踪迁移:

DL is deprecated, please use Fiddle
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:migrate
-- add_index(:performer_performances, :performer_id)
-- add_index(:performer_performances, :performer_id)
rake aborted!
SQLite3::SQLException: no such table: main.performer_performances: CREATE  INDEX
 "index_performer_performances_on_performer_id" ON "performer_performances" ("pe
rformer_id")
C:/Ruby200/lib/ruby/gems/2.0.0/gems/sqlite3-1.3.8-x86-mingw32/lib/sqlite3/databa
se.rb:91:in `initialize'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/sqlite3-1.3.8-x86-mingw32/lib/sqlite3/databa
se.rb:91:in `new'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/sqlite3-1.3.8-x86-mingw32/lib/sqlite3/databa
se.rb:91:in `prepare'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/sqlite3-1.3.8-x86-mingw32/lib/sqlite3/databa
se.rb:134:in `execute'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.0.3/lib/active_record/connect
ion_adapters/sqlite3_adapter.rb:331:in `block in execute'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.0.3/lib/active_record/connect
ion_adapters/abstract_adapter.rb:435:in `block in log'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/activesupport-4.0.3/lib/active_support/notif
ications/instrumenter.rb:20:in `instrument'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.0.3/lib/active_record/connect
ion_adapters/abstract_adapter.rb:430:in `log'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.0.3/lib/active_record/connect
ion_adapters/sqlite3_adapter.rb:331:in `execute'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.0.3/lib/active_record/connect
ion_adapters/abstract/schema_statements.rb:522:in `add_index'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.0.3/lib/active_record/migrati
on.rb:625:in `block in method_missing'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.0.3/lib/active_record/migrati
on.rb:597:in `block in say_with_time'
C:/Ruby200/lib/ruby/2.0.0/benchmark.rb:281:in `measure'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.0.3/lib/active_record/migrati
on.rb:597:in `say_with_time'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.0.3/lib/active_record/migrati
on.rb:617:in `method_missing'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.0.3/lib/active_record/migrati
on.rb:383:in `method_missing'
C:/rubyProjects/dads/db/migrate/20140315155732_create_performer_performances.rb:
9:in `<class:CreatePerformerPerformances>'
C:/rubyProjects/dads/db/migrate/20140315155732_create_performer_performances.rb:
1:in `<top (required)>'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/activesupport-4.0.3/lib/active_support/depen
dencies.rb:229:in `require'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/activesupport-4.0.3/lib/active_support/depen
dencies.rb:229:in `block in require'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/activesupport-4.0.3/lib/active_support/depen
dencies.rb:214:in `load_dependency'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/activesupport-4.0.3/lib/active_support/depen
dencies.rb:229:in `require'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.0.3/lib/active_record/migrati
on.rb:718:in `load_migration'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.0.3/lib/active_record/migrati
on.rb:714:in `migration'
C:131071:in `disable_ddl_transaction'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.0.3/lib/active_record/migrati
on.rb:1012:in `use_transaction?'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.0.3/lib/active_record/migrati
on.rb:922:in `rescue in block in migrate'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.0.3/lib/active_record/migrati
on.rb:919:in `block in migrate'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.0.3/lib/active_record/migrati
on.rb:916:in `each'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.0.3/lib/active_record/migrati
on.rb:916:in `migrate'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.0.3/lib/active_record/migrati
on.rb:764:in `up'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.0.3/lib/active_record/migrati
on.rb:742:in `migrate'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/activerecord-4.0.3/lib/active_record/railtie
s/databases.rake:42:in `block (2 levels) in <top (required)>'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/task.rb:236:in `call'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/task.rb:236:in `block i
n execute'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/task.rb:231:in `each'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/task.rb:231:in `execute
'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/task.rb:175:in `block i
n invoke_with_call_chain'
C:/Ruby200/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/task.rb:168:in `invoke_
with_call_chain'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/task.rb:161:in `invoke'

C:/Ruby200/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:149:in `
invoke_task'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:106:in `
block (2 levels) in top_level'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:106:in `
each'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:106:in `
block in top_level'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:115:in `
run_with_threads'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:100:in `
top_level'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:78:in `b
lock in run'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:165:in `
standard_exception_handling'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/rake-10.1.1/lib/rake/application.rb:75:in `r
un'
C:/Ruby200/lib/ruby/gems/2.0.0/gems/rake-10.1.1/bin/rake:33:in `<top (required)>
'
C:/Ruby200/bin/rake:23:in `load'
C:/Ruby200/bin/rake:23:in `<main>'
Tasks: TOP => db:migrate

我知道我可能编辑了一些导致错误的文件但我不知道在哪里看。如果您需要其他信息或文件,请询问并生病发布。

1 个答案:

答案 0 :(得分:4)

您需要在change方法

中包含所有迁移功能
class CreatePerformerPerformances < ActiveRecord::Migration

  def change

    create_table :performer_performances do |t|
      t.integer :performer_id
      t.integer :performance_id
      t.timestamps
    end

    add_index :performer_performances, :performer_id
    add_index :performer_performances, :performance_id
    add_index :performer_performances, [:performer_id, :performance_id], unique: true
  end
end