迁移/模型问题PG :: UndefinedTable:错误:关系“教师”不存在

时间:2016-07-27 19:06:04

标签: ruby-on-rails devise

您好我一直在尝试将一个rails应用程序部署到Heroku,这是一个使用Devise的学生 - 教师交流平台。该应用程序在本地运行正常,但我无法运行迁移,所以虽然主页等在heroku上工作正常,但当我点击注册它不起作用(PG :: UndefinedTable:ERROR ....)

我对迁移文件做了一些更改,试图更接近解决问题,现在它不允许我运行rake db:本地迁移。

学生和教师是设计用户的类型。英语成绩和数学成绩属于一名教师和一名学生,教师/学生可以拥有许多这些成绩。任务和生成的反馈属于教师。

rake db:migrate produces the following errors. 
== 20160714153750 CreateGeneratedFeedbacks: migrating =========================
-- create_table(:generated_feedbacks)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

PG::UndefinedTable: ERROR:  relation "teachers" does not exist
: CREATE TABLE "generated_feedbacks" ("id" serial primary key, "performance_grade" character varying, "teacher_id" integer, "subject" character varying, "auto_feedback" text, "subcategory" character varying, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL, CONSTRAINT "fk_rails_ee351f38a1"
FOREIGN KEY ("teacher_id")
  REFERENCES "teachers" ("id")
)
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:98:in `async_exec'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:98:in `block in execute'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:566:in `block in log'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:560:in `log'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:97:in `execute'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract/schema_statements.rb:283:in `create_table'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:845:in `block in method_missing'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:814:in `block in say_with_time'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:814:in `say_with_time'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:834:in `method_missing'
/Users/Jack/programming/ruby/second_attempt/second_attempt/db/migrate/20160714153750_create_generated_feedbacks.rb:4:in `change'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:788:in `exec_migration'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:772:in `block (2 levels) in migrate'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:771:in `block in migrate'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:398:in `with_connection'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:770:in `migrate'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:950:in `migrate'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1211:in `block in execute_migration_in_transaction'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1279:in `block in ddl_transaction'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:232:in `block in transaction'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract/transaction.rb:189:in `within_new_transaction'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:232:in `transaction'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/transactions.rb:211:in `transaction'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1279:in `ddl_transaction'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1210:in `execute_migration_in_transaction'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1183:in `block in migrate_without_lock'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1182:in `each'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1182:in `migrate_without_lock'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1131:in `block in migrate'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1298:in `with_advisory_lock'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1131:in `migrate'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1005:in `up'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:983:in `migrate'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/tasks/database_tasks.rb:161:in `migrate'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/railties/databases.rake:58:in `block (2 levels) in <top (required)>'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/rake-11.2.2/exe/rake:27:in `<top (required)>'
/Users/Jack/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `eval'
/Users/Jack/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `<main>'
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "teachers" does not exist
: CREATE TABLE "generated_feedbacks" ("id" serial primary key, "performance_grade" character varying, "teacher_id" integer, "subject" character varying, "auto_feedback" text, "subcategory" character varying, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL, CONSTRAINT "fk_rails_ee351f38a1"
FOREIGN KEY ("teacher_id")
  REFERENCES "teachers" ("id")
)
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:98:in `async_exec'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:98:in `block in execute'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:566:in `block in log'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:560:in `log'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:97:in `execute'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract/schema_statements.rb:283:in `create_table'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:845:in `block in method_missing'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:814:in `block in say_with_time'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:814:in `say_with_time'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:834:in `method_missing'
/Users/Jack/programming/ruby/second_attempt/second_attempt/db/migrate/20160714153750_create_generated_feedbacks.rb:4:in `change'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:788:in `exec_migration'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:772:in `block (2 levels) in migrate'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:771:in `block in migrate'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:398:in `with_connection'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:770:in `migrate'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:950:in `migrate'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1211:in `block in execute_migration_in_transaction'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1279:in `block in ddl_transaction'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:232:in `block in transaction'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract/transaction.rb:189:in `within_new_transaction'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:232:in `transaction'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/transactions.rb:211:in `transaction'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1279:in `ddl_transaction'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1210:in `execute_migration_in_transaction'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1183:in `block in migrate_without_lock'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1182:in `each'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1182:in `migrate_without_lock'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1131:in `block in migrate'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1298:in `with_advisory_lock'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1131:in `migrate'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1005:in `up'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:983:in `migrate'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/tasks/database_tasks.rb:161:in `migrate'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/railties/databases.rake:58:in `block (2 levels) in <top (required)>'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/rake-11.2.2/exe/rake:27:in `<top (required)>'
/Users/Jack/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `eval'
/Users/Jack/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `<main>'
PG::UndefinedTable: ERROR:  relation "teachers" does not exist
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:98:in `async_exec'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:98:in `block in execute'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:566:in `block in log'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activesupport-5.0.0/lib/active_support/notifications/instrumenter.rb:21:in `instrument'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:560:in `log'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/postgresql/database_statements.rb:97:in `execute'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract/schema_statements.rb:283:in `create_table'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:845:in `block in method_missing'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:814:in `block in say_with_time'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:814:in `say_with_time'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:834:in `method_missing'
/Users/Jack/programming/ruby/second_attempt/second_attempt/db/migrate/20160714153750_create_generated_feedbacks.rb:4:in `change'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:788:in `exec_migration'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:772:in `block (2 levels) in migrate'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:771:in `block in migrate'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:398:in `with_connection'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:770:in `migrate'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:950:in `migrate'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1211:in `block in execute_migration_in_transaction'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1279:in `block in ddl_transaction'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:232:in `block in transaction'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract/transaction.rb:189:in `within_new_transaction'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:232:in `transaction'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/transactions.rb:211:in `transaction'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1279:in `ddl_transaction'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1210:in `execute_migration_in_transaction'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1183:in `block in migrate_without_lock'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1182:in `each'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1182:in `migrate_without_lock'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1131:in `block in migrate'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1298:in `with_advisory_lock'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1131:in `migrate'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:1005:in `up'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/migration.rb:983:in `migrate'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/tasks/database_tasks.rb:161:in `migrate'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/activerecord-5.0.0/lib/active_record/railties/databases.rake:58:in `block (2 levels) in <top (required)>'
/Users/Jack/.rvm/gems/ruby-2.3.0/gems/rake-11.2.2/exe/rake:27:in `<top (required)>'
/Users/Jack/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `eval'
/Users/Jack/.rvm/gems/ruby-2.3.0/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => db:migrate

设计用户迁移

class DeviseCreateUsers < ActiveRecord::Migration[5.0]
  def change

    create_table :users do |t|
      ## Database authenticatable
      t.string :email,              null: false, default: ""
      t.string :encrypted_password, null: false, default: ""

      #adding
      t.string :type
      t.string :teacher_name
      t.string :teacher_email
      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      t.integer  :sign_in_count, default: 0, null: false
      t.datetime :current_sign_in_at
      t.datetime :last_sign_in_at
      t.string   :current_sign_in_ip
      t.string   :last_sign_in_ip



      #additional values
      t.string :first_name
      t.string :last_name


      ## Confirmable
      # t.string   :confirmation_token
      # t.datetime :confirmed_at
      # t.datetime :confirmation_sent_at
      # t.string   :unconfirmed_email # Only if using reconfirmable

      ## Lockable
      # t.integer  :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at


      t.timestamps null: false
    end

    add_index :users, :email,                unique: true
    add_index :users, :reset_password_token, unique: true
    # add_index :users, :confirmation_token,   unique: true
    # add_index :users, :unlock_token,         unique: true
  end
end

英语成绩(或多或少与数学相同)

class CreateEnglishGrades < ActiveRecord::Migration[5.0]
  def change

    create_table :english_grades do |t|
      t.references :teacher, foreign_key: true
      t.references :student, foreign_key: true
      t.string :title
      t.string :subcategory
      t.string :performance_grade
      t.string :feedback
      t.date :grade_date

      t.timestamps
    end
  end
end

生成的反馈:     class CreateGeneratedFeedbacks&lt; ActiveRecord的::迁移[5.0]       改变

    create_table :generated_feedbacks do |t|
      t.string :performance_grade
      t.references :teacher, foreign_key: true
      t.string :subject
      t.text :auto_feedback
      t.string :subcategory

      t.timestamps
    end
  end
end

任务:

 class CreateTasks < ActiveRecord::Migration[5.0]
      def change
        create_table :tasks do |t|
          t.string :subject
          t.text :task
          t.references :teacher, foreign_key: true
          t.date :due_date
          t.text :description

          t.timestamps
        end
      end
    end

模型

英语成绩(与数学成绩相同)

class EnglishGrade < ApplicationRecord
  belongs_to :teacher
  belongs_to :student
end

生成的反馈

class GeneratedFeedback < ApplicationRecord
  belongs_to :teacher
end

用户

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
end

学生

class Student < User
    has_many :grades
    has_many :maths_grades
    has_many :english_grades
    has_many :generated_feedback
end

教师

class Teacher < User
    has_many :grades
    has_many :maths_grades
    has_many :english_grades
    has_many :generated_feedback
    has_many :task
end

架构:

ActiveRecord::Schema.define(version: 20160713100728) do

  # These are extensions that must be enabled in order to support this database
  enable_extension "plpgsql"

  create_table "english_grades", force: :cascade do |t|
    t.integer  "teacher_id"
    t.integer  "student_id"
    t.string   "title"
    t.string   "subcategory"
    t.string   "performance_grade"
    t.string   "feedback"
    t.date     "grade_date"
    t.datetime "created_at",        null: false
    t.datetime "updated_at",        null: false
    t.index ["student_id"], name: "index_english_grades_on_student_id", using: :btree
    t.index ["teacher_id"], name: "index_english_grades_on_teacher_id", using: :btree
  end

  create_table "grades", force: :cascade do |t|
    t.integer  "student_id"
    t.integer  "teacher_id"
    t.text     "feedback"
    t.integer  "percentage_score"
    t.string   "grade_type"
    t.string   "grade_letter"
    t.date     "grade_date"
    t.string   "subject"
    t.datetime "created_at",       null: false
    t.datetime "updated_at",       null: false
    t.index ["student_id"], name: "index_grades_on_student_id", using: :btree
    t.index ["teacher_id"], name: "index_grades_on_teacher_id", using: :btree
  end

  create_table "maths_grades", force: :cascade do |t|
    t.integer  "teacher_id"
    t.integer  "student_id"
    t.string   "title"
    t.string   "subcategory"
    t.string   "performance_grade"
    t.string   "feedback"
    t.date     "grade_date"
    t.datetime "created_at",        null: false
    t.datetime "updated_at",        null: false
    t.index ["student_id"], name: "index_maths_grades_on_student_id", using: :btree
    t.index ["teacher_id"], name: "index_maths_grades_on_teacher_id", using: :btree
  end

  create_table "users", force: :cascade do |t|
    t.string   "email",                  default: "", null: false
    t.string   "encrypted_password",     default: "", null: false
    t.string   "reset_password_token"
    t.datetime "reset_password_sent_at"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",          default: 0,  null: false
    t.datetime "current_sign_in_at"
    t.datetime "last_sign_in_at"
    t.string   "current_sign_in_ip"
    t.string   "last_sign_in_ip"
    t.string   "first_name"
    t.string   "last_name"
    t.datetime "created_at",                          null: false
    t.datetime "updated_at",                          null: false
    t.string   "type"
    t.string   "Teacher_name"
    t.string   "teacher_email"
    t.index ["email"], name: "index_users_on_email", unique: true, using: :btree
    t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
  end

end

非常感谢,我已经坚持了这个问题将近一个星期,所以任何帮助都将非常感激。

可能相关或不相关的额外信息:

- 我认为这可能是我的迁移文件的问题,因为我之前有一些添加了额外的列。为了尝试解决这个问题,我最近删除了这些额外的迁移文件,并更新了现有迁移文件中所需的列。

1 个答案:

答案 0 :(得分:1)

您正在为UserStudentTeacher模型使用单表继承。虽然模型本身看起来很好,但迁移缺乏正确处理关联的信息。

当你这样做时

t.references :teacher, foreign_key: true

Rails尝试创建一个外键,引用不存在的teachers表。

…CONSTRAINT "fk_rails_ee351f38a1"
  FOREIGN KEY ("teacher_id") REFERENCES "teachers" ("id")…

您应该定义这样的引用:

create_table :generated_feedbacks do |t|
  t.references :teacher, references: :users
  …
end

CreateEnglishGrades应使用类似的方法:

create_table :english_grades do |t|
  t.references :teacher, references: :users
  t.references :student, references: :users
  …
end

然后需要单独定义外键:

add_foreign_key :english_grades, :users, column: :teacher_id
add_foreign_key :english_grades, :users, column: :student_id