【问题标题】:adding a foreign key in the table在表中添加外键
【发布时间】:2011-06-11 00:49:14
【问题描述】:

我不断收到此异常:"SQLite3::SQLException: no such column: books.user_id: SELECT "books".* FROM "books" WHERE ("books".user_id = 4)"。这听起来像books 表中没有user_id。

所以我只是安装了Foreigner插件,并在图书迁移文件中添加了"t.integer :user_id, :null => false""add_foreign_key(:books, :users)"。我跑了"rake db:migrate",但它仍然给了我同样的例外。

我在 Windows 中使用 Rails 3 并设计来验证用户。

首页视图

  <p><%= link_to "Add new Book",:controller =>"book", :action => 'new' %></p>
   <% @books.each do |b| %>
  <p><%= b.author%></p>
  <p><%= b.title%></p>
  <%end%>

家庭控制器

   class HomeController < ApplicationController
   def index
  @user = current_user
  @user.books||=Book.new
  @books=@user.books
  end
  end

图书控制器

    class BookController < ApplicationController
  def new
 @books = Book.new
   # redirect_to :controller=>"home" ,:action=>"index"
  end

    def create
   @books = Book.new(params[:book])
   if @books.save
    render "home/index"
   #redirect_to :controller=>"home" ,:action=>"index"
   else

    render :action => 'new'
   end
  end

创建表/书迁移

  class CreateBooks < ActiveRecord::Migration
  def self.up
  create_table :books do |t|
  t.text :title
  t.text :author
  t.integer :user_id, :null => false
  t.timestamps
 end
 add_foreign_key(:books, :users)
end

预订视图

<h1>Book#new</h1>

<%= form_for(:book) do |f| %>
<p><%= f.text_field :title %></p>
 <p><%= f.text_field :author %></p>
 <p><%= f.submit "Add book"%> 

书籍模型

 class Book < ActiveRecord::Base
 belongs_to :user
 end

用户模型

class User < ActiveRecord::Base
has_many :books
# Include default devise modules. Others available are:
# :token_authenticatable, :lockable, :timeoutable and :activatable
devise :database_authenticatable, :registerable,
 :recoverable, :rememberable, :trackable, :validatable

 # Setup accessible (or protected) attributes for your model
 attr_accessible :email, :password,             :password_confirmation,:firstname,:lastname,:school,:major,:sex,:zipcode


  end

路线 Campus::Application.routes.draw 做 获取“书/索引”

  get "book/edit"

   get "book/new"

   get "home/edit"

  devise_for :users
   resources :book     
  root :to=> "home#index"
  match '/book/new' =>"home#index"
   end

数据库架构

    ActiveRecord::Schema.define(:version => 20110609055608) do

  create_table "books", :force => true do |t|
    t.text     "title"
    t.text     "author"
    t.integer  "user_id",    :null => false
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "courses", :force => true do |t|
    t.string   "name"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "strong_ins", :force => true do |t|
    t.string   "subject"
    t.string   "topic"
    t.text     "description"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

  create_table "users", :force => true do |t|
    t.string   "email",                               :default => "", :null => false
    t.string   "encrypted_password",   :limit => 128, :default => "", :null => false
    t.string   "password_salt",                       :default => "", :null => false
    t.string   "reset_password_token"
    t.string   "remember_token"
    t.datetime "remember_created_at"
    t.integer  "sign_in_count",                       :default => 0
    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.datetime "created_at"
    t.datetime "updated_at"
    t.string   "firstname"
    t.string   "lastname"
    t.text     "school"
    t.text     "major"
    t.string   "sex"
    t.integer  "zipcode"
  end

  add_index "users", ["email"], :name => "index_users_on_email", :unique => true
  add_index "users", ["reset_password_token"], :name => "index_users_on_reset_password_token", :unique => true

  create_table "weak_ins", :force => true do |t|
    t.string   "subject"
    t.string   "topic"
    t.text     "description"
    t.datetime "created_at"
    t.datetime "updated_at"
  end

end

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 foreign-keys foreigner


    【解决方案1】:

    运行迁移后,user_id 列应出现在架构中。它不在您的列表中,所以我会说这就是问题所在。确保 rake db:migrate 完成时没有错误。如有必要,您可以使用 rake db:rollback &amp;&amp; rake db:migrate 重新进行迁移。

    【讨论】:

    • 我刚刚跑了 rake db:rollback 然后 rake db:migrate 再次给我同样的异常
    • 你能检查一下log/development.log,看看是否正在记录SQL语句吗?
    • 想一想,foreigner 可能不适用于 SQLite。尝试删除 add_foreign_key 语句并再次运行迁移。
    • 好的,我已经能够添加外键,但是这本书没有出现在主页/索引视图中。这是 dev.log 文件中的信息 Load (3.0ms)[0m SELECT "users ".* FROM "users" WHERE "users"."id" = 4 LIMIT 1 [1m[36mBook Load (1.0ms)[0m [1mSELECT "books".* FROM "books" WHERE ("books".user_id = 4 )[0m
    猜你喜欢
    • 1970-01-01
    • 2020-08-28
    • 2019-01-11
    • 1970-01-01
    • 2018-07-19
    • 2023-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多