I'm getting an error:
我收到一个错误:
SQLite3::SQLException: no such column: ideas.list_id:
SELECT "ideas".* FROM "ideas"
WHERE "ideas"."list_id" = 2
But I added
但我补充道
t.integer :list_id
t.integer:list_id
to my db migration file:
到我的数据库迁移文件:
class CreateIdeas < ActiveRecord::Migration
def change
create_table :ideas do |t|
t.string :name
t.text :description
t.string :picture
t.timestamps
end
add_foreign_key :ideas, :lists
end
end
which gave me this:
这给了我这个:
class CreateIdeas < ActiveRecord::Migration
def change
create_table :ideas do |t|
t.string :name
t.text :description
t.string :picture
t.integer :list_id
t.timestamps
end
add_foreign_key :ideas, :lists
end
end
and then I typed
然后我输入
rake db:migrate
Any idea why I would be getting an error saying there's no column? I'm still new to RoRs. Do I have to add a column some other way?
知道为什么我会收到一个错误,说没有列?我还是RoR的新手。我是否必须以其他方式添加列?
Thanks
谢谢
7 个解决方案
#1
52
As Speransky suggested, you should never modify old migration files. Rather you should create a new migration that adds the desired column. For instance, in this case you would run the following in your app to create the new migration:
正如Speransky建议的那样,您永远不应该修改旧的迁移文件。相反,您应该创建一个添加所需列的新迁移。例如,在这种情况下,您将在应用程序中运行以下命令以创建新的迁移:
rails generate migration AddListIdColumnToIdeas list_id:integer
And Rails would generate the migration file automatically and the only thing left to do is run rake db:migrate
.
Rails会自动生成迁移文件,剩下要做的就是运行rake db:migrate。
If you insist on modifying the old migration file, you can add the column as you did and run the following:
如果您坚持修改旧的迁移文件,则可以像添加该列一样添加该列并运行以下命令:
rake db:drop
rake db:create
rake db:migrate
Which will destroy your current database, create a new one and run all the migrations (which will include your new column).
这将破坏您当前的数据库,创建一个新数据库并运行所有迁移(包括您的新列)。
#2
3
If you want to add a new column to an exist database, you should use rails generate migration
. So you can try rails generate migration add_list_id_to_ideas list_id:integer
and then use rake db:migrate
to commit this change.
如果要向现有数据库添加新列,则应使用rails generate migration。所以你可以尝试rails生成迁移add_list_id_to_ideas list_id:integer然后使用rake db:migrate来提交这个更改。
#3
2
You should not add new rows to old migrations. Migration is a step of building database. And number of last executed migration is stored in schema
, and it will not be run or redone if you use will use rake db:migrate
. If you run the migration with creating the table before, then you should create new migration where you may use add_column
method.
您不应该向旧迁移添加新行。迁移是构建数据库的一个步骤。并且最后执行的迁移的数量存储在模式中,如果使用它将不会运行或重做,将使用rake db:migrate。如果您之前通过创建表来运行迁移,那么您应该创建新的迁移,您可以使用add_column方法。
#4
1
migration file name has the datetime encoded in its name so rails run this migration one and do not run it again unless you do a rollback
迁移文件名具有以其名称编码的日期时间,因此rails运行此迁移,并且除非您执行回滚,否则不会再次运行它
and here come the magic of migration to build you db with small steps so no need to update a migration after run rake db:migrate , you should make a new migration to do the change you want to your db schema
以下是迁移的神奇之处,可以通过小步骤构建数据库,因此在运行rake db:migrate之后无需更新迁移,您应该进行新的迁移以对数据库模式进行更改
and remember to
remove the added line form the old migration file as it might raise errors if you decided to rollback this migration
从旧的迁移文件中删除添加的行,因为如果您决定回滚此迁移,它可能会引发错误
#5
1
Rails 4.0 easy way to add single or multiple column https://gist.github.com/pyk/8569812
Rails 4.0简单的方法来添加单列或多列https://gist.github.com/pyk/8569812
#6
0
If you already have files in your migrate folder, you could just add column you want there(just type the code), delete development.sqlite or whatever represents your db file, and run rake db:migrate. It will then create a new sqlite file with new column in table, and you can check it in schema.rb
如果您的migrate文件夹中已有文件,则只需添加所需的列(只需键入代码),删除development.sqlite或代表db文件的任何文件,然后运行rake db:migrate。然后它将在表中创建一个带有新列的新sqlite文件,您可以在schema.rb中进行检查
So, basically, everything you did seems good, except you didn't delete your database file. Doing this seems the easiest for me, all though you will lose all the files in your database. If you're just testing and developing Rails app, this works. Can anyone comment if there is something wrong with this approach, besides what i wrote?
所以,基本上,你做的一切似乎都很好,除了你没有删除你的数据库文件。这样做对我来说似乎最简单,尽管你会丢失数据库中的所有文件。如果你只是测试和开发Rails应用程序,这是有效的。除了我写的内容之外,有人可以评论这种方法是否有问题吗?
Edit: I actually found an answer about that here Editing Existing Rails Migrations is a good idea?
编辑:我实际上在这里找到了一个答案编辑现有Rails迁移是一个好主意?
#7
0
You can also do this ..
你也可以这样做..
rails g migration add_column_to_users list_id:string
rails g migration add_column_to_users list_id:string
then rake db:migrate
然后rake db:migrate
also add :list_id attribute in your user controller ;
还在用户控制器中添加:list_id属性;
for more detail check out http://guides.rubyonrails.org/active_record_migrations.html
有关更多详细信息,请访问http://guides.rubyonrails.org/active_record_migrations.html
#1
52
As Speransky suggested, you should never modify old migration files. Rather you should create a new migration that adds the desired column. For instance, in this case you would run the following in your app to create the new migration:
正如Speransky建议的那样,您永远不应该修改旧的迁移文件。相反,您应该创建一个添加所需列的新迁移。例如,在这种情况下,您将在应用程序中运行以下命令以创建新的迁移:
rails generate migration AddListIdColumnToIdeas list_id:integer
And Rails would generate the migration file automatically and the only thing left to do is run rake db:migrate
.
Rails会自动生成迁移文件,剩下要做的就是运行rake db:migrate。
If you insist on modifying the old migration file, you can add the column as you did and run the following:
如果您坚持修改旧的迁移文件,则可以像添加该列一样添加该列并运行以下命令:
rake db:drop
rake db:create
rake db:migrate
Which will destroy your current database, create a new one and run all the migrations (which will include your new column).
这将破坏您当前的数据库,创建一个新数据库并运行所有迁移(包括您的新列)。
#2
3
If you want to add a new column to an exist database, you should use rails generate migration
. So you can try rails generate migration add_list_id_to_ideas list_id:integer
and then use rake db:migrate
to commit this change.
如果要向现有数据库添加新列,则应使用rails generate migration。所以你可以尝试rails生成迁移add_list_id_to_ideas list_id:integer然后使用rake db:migrate来提交这个更改。
#3
2
You should not add new rows to old migrations. Migration is a step of building database. And number of last executed migration is stored in schema
, and it will not be run or redone if you use will use rake db:migrate
. If you run the migration with creating the table before, then you should create new migration where you may use add_column
method.
您不应该向旧迁移添加新行。迁移是构建数据库的一个步骤。并且最后执行的迁移的数量存储在模式中,如果使用它将不会运行或重做,将使用rake db:migrate。如果您之前通过创建表来运行迁移,那么您应该创建新的迁移,您可以使用add_column方法。
#4
1
migration file name has the datetime encoded in its name so rails run this migration one and do not run it again unless you do a rollback
迁移文件名具有以其名称编码的日期时间,因此rails运行此迁移,并且除非您执行回滚,否则不会再次运行它
and here come the magic of migration to build you db with small steps so no need to update a migration after run rake db:migrate , you should make a new migration to do the change you want to your db schema
以下是迁移的神奇之处,可以通过小步骤构建数据库,因此在运行rake db:migrate之后无需更新迁移,您应该进行新的迁移以对数据库模式进行更改
and remember to
remove the added line form the old migration file as it might raise errors if you decided to rollback this migration
从旧的迁移文件中删除添加的行,因为如果您决定回滚此迁移,它可能会引发错误
#5
1
Rails 4.0 easy way to add single or multiple column https://gist.github.com/pyk/8569812
Rails 4.0简单的方法来添加单列或多列https://gist.github.com/pyk/8569812
#6
0
If you already have files in your migrate folder, you could just add column you want there(just type the code), delete development.sqlite or whatever represents your db file, and run rake db:migrate. It will then create a new sqlite file with new column in table, and you can check it in schema.rb
如果您的migrate文件夹中已有文件,则只需添加所需的列(只需键入代码),删除development.sqlite或代表db文件的任何文件,然后运行rake db:migrate。然后它将在表中创建一个带有新列的新sqlite文件,您可以在schema.rb中进行检查
So, basically, everything you did seems good, except you didn't delete your database file. Doing this seems the easiest for me, all though you will lose all the files in your database. If you're just testing and developing Rails app, this works. Can anyone comment if there is something wrong with this approach, besides what i wrote?
所以,基本上,你做的一切似乎都很好,除了你没有删除你的数据库文件。这样做对我来说似乎最简单,尽管你会丢失数据库中的所有文件。如果你只是测试和开发Rails应用程序,这是有效的。除了我写的内容之外,有人可以评论这种方法是否有问题吗?
Edit: I actually found an answer about that here Editing Existing Rails Migrations is a good idea?
编辑:我实际上在这里找到了一个答案编辑现有Rails迁移是一个好主意?
#7
0
You can also do this ..
你也可以这样做..
rails g migration add_column_to_users list_id:string
rails g migration add_column_to_users list_id:string
then rake db:migrate
然后rake db:migrate
also add :list_id attribute in your user controller ;
还在用户控制器中添加:list_id属性;
for more detail check out http://guides.rubyonrails.org/active_record_migrations.html
有关更多详细信息,请访问http://guides.rubyonrails.org/active_record_migrations.html