简体   繁体   English

Ruby on Rails-rake db:seed中止了

[英]Ruby on Rails - rake db:seed aborted

I added to my app the devise gem for logIn. 我在应用程序中添加了用于登录的设计工具。 My page is a shop and one user has many orders and I add in model user: 我的页面是一家商店,一个用户有很多订单,我添加了模型用户:

class User < ApplicationRecord
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
  has_many :comandas    #I add this line, comandas = orders in Spanish
end

And in the migrate file of Comandas(orders) I added this: 在Comandas(订单)的迁移文件中,我添加了以下内容:

class CreateComandas < ActiveRecord::Migration[5.0]
  def change
    create_table :comandas do |t|
        t.belongs_to :user, index: true #references to user
        t.float   :total
      t.timestamps
    end
  end
end

And now I'm testing the DB with seeds file but when I add a Comanda to a User this raise me the error that rake is aborted. 现在,我正在使用种子文件测试数据库,但是当我向用户添加Comanda时,这会引起我rake中止的错误。

My seeds file: 我的种子文件:

c1 = Comanda.create(total: 100)
u1 = User.find_by email: "aaa@aaa.cat"
u1.comandas << [c1]

I'created the user before. 我之前创建过用户。

Error: Screen shot of shell 错误: 外壳的屏幕截图

UPDATE: 更新:

I finally found the problem. 我终于找到了问题。 The problem was that I created all models before add this modification. 问题是我在添加此修改之前创建了所有模型。 And when I was adding this relation, Rails didn't change me the database. 当我添加此关系时,Rails并没有更改我的数据库。 I checked this going into the sqlite command line and listing the columns of Comandas table. 我检查了这一点,进入sqlite命令行并列出了Comandas表的列。 And I saw that the reference to user wasn't here. 我发现这里没有提到用户。 And decide to drop all database executing this command: 并决定删除所有执行此命令的数据库:

rake db:drop db:create db:migrate

And after I added a user and I run: 在添加用户并运行后:

rake db:seed 

And works! 和工程! Thanks to all for the help! 感谢大家的帮助!

Add user_id in comandas table. comandas表中添加user_id

Add new migration to add user_id : 添加新迁移以添加user_id

rails g migration AddUserIdToCommando user_id:integer

The Comanda model is not enclosed, but I can guess the possible reason for this behavior. 没有随附Comanda模型,但是我可以猜测出这种现象的可能原因。

First, what will probably work, and is more compatible with the actual underlying db model is: 首先,可能有效并且与实际基础数据库模型更兼容的是:

c1 = Comanda.new(total: 100)
u1 = User.find_by email: "aaa@aaa.cat"
c1.user_id = u1.id
c1.save!

The User model has no idea of it's child's foreign key, the Comandas table's 'user_id' column. 用户模型不知道它是孩子的​​外键,即Comandas表的“ user_id”列。 It's more intuitive to see that the Comanda model is responsible for saving its own db columns. 更加直观地看到Comanda模型负责保存其自己的db列。

However I can guess, that due to the fact that recent versions of Rails enforce the belongs_to relation by default, when you try to do - 但是我可以猜测,由于以下事实,当您尝试执行以下操作时,默认情况下,最新版本的Rails会强制执行Emirates_to关系-

c1 = Comanda.create(total: 100)

it fails, since by using Comanda.create rather than Comanda.new, you're attempting to create and save a new Comanda object with a nil User relation. 它失败了,因为通过使用Comanda.create而不是Comanda.new,您正在尝试创建和保存具有零用户关系的新Comanda对象。 You can overcome this by specifying: 您可以通过指定以下内容来克服此问题:

belongs_to :user, :optional => true

in your Comanda model, but this will probably break your business model. 在您的Comanda模型中,但这可能会破坏您的业务模型。

The actual error is due to the fact that the first line (create c1) failed, and when processing gets to the last line (commandas << ...), the c1 object is nil or invalid and then the update to the user object fails. 实际错误是由于以下事实:第一行(创建c1)失败,并且当处理到达最后一行(commandas << ...)时,c1对象为nil或无效,然后更新了用户对象失败。

Hope this helps! 希望这可以帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM