在表中添加外键

发布于 2024-11-15 02:16:39 字数 4541 浏览 1 评论 0原文

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

所以我只是安装了Foreigner插件并在书籍迁移文件。我运行了“rake db:migrate”,但它仍然给了我同样的异常。

我在 Windows 和 Devise 中使用 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
  @[email protected]
  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

I keep getting this exception: "SQLite3::SQLException: no such column: books.user_id: SELECT "books".* FROM "books" WHERE ("books".user_id = 4)". Which sounds like there is no user_id in the books table.

So I just installed the Foreigner plugin and added "t.integer :user_id, :null => false" and "add_foreign_key(:books, :users)" in the book migration file. I ran "rake db:migrate", but still it is giving me the same exception.

I am using Rails 3 in Windows and Devise to authenticate user.

HOME VIEW

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

HOME CONTROLLER

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

BOOK CONTROLLER

    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

CREATE TABLE/BOOK MIGRATION

  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

BOOK VIEW

<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"%> 

BOOK MODEL

 class Book < ActiveRecord::Base
 belongs_to :user
 end

USER MODEL

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

ROUTE
Campus::Application.routes.draw do
get "book/index"

  get "book/edit"

   get "book/new"

   get "home/edit"

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

DATABASE SCHEMA

    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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吃兔兔 2024-11-22 02:16:39

运行迁移后,user_id 列应出现在架构中。它不在您的列表中,所以我认为这就是问题所在。确保 rake db:migrate 顺利完成。您可以使用 rake db:rollback && 重做迁移rake db:migrate(如有必要)。

The user_id column should appear in the schema after running the migration. It's not in your listing, so I'd say that's the problem. Make sure rake db:migrate is completing without errors. You can redo the migration with rake db:rollback && rake db:migrate, if necessary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文