当从一台计算机移动到另一台计算机时,我使用了gem,seed_dump来转储和播种我的数据库。虽然我可以在播种时转储数据,但我遇到了以下错误:
SyntaxError: /Users/bowser/rails_projects/project_one/db/seeds.rb:7: syntax error, unexpected ']', expecting end-of-input
/Library/Ruby/Gems/2.0.0/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:241:in `load'
/Library/Ruby/Gems/2.0.0/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:241:in `block in load'
/Library/Ruby/Gems/2.0.0/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:232:in `load_dependency'
/Library/Ruby/Gems/2.0.0/gems/activesupport-4.1.0/lib/active_support/dependencies.rb:241:in `load'
/Library/Ruby/Gems/2.0.0/gems/railties-4.1.0/lib/rails/engine.rb:543:in `load_seed'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.0/lib/active_record/tasks/database_tasks.rb:184:in `load_seed'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.1.0/lib/active_record/railties/databases.rake:173:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:seed
(See full trace by running task with --trace)
除了创建文件外,没有更改任何代码,这使我想知道是否从一开始就正确地输出了文件。
种子文件
Article.create!([
{title: "RailsConf", body: "RailsConf is the official gathering for Rails developers..", published_at: "2013-04-13 00:00:00", excerpt: nil, location: nil, user_id: nil},
{title: "Introduction to Active Record", body: "Active Record is Rail's default ORM..", published_at: "2014-08-14 00:00:00", excerpt: nil, location: nil, user_id: nil}
])
#<Class:0x007f92d712a108>.create!([
{article_id: 1, category_id: 1}
])
Category.create!([
{name: "sports"}
])
#<Class:0x007f92d70e2268>.create!([
{article_id: 1, category_id: 1}
])
Profile.create!([
{user_id: nil, name: "Joe", birthday: "2014-08-14", color: "blue", twitter: "twitter.com/joe"}
])
坏消息!
seed_dump
对于没有相应模型的表不起作用,例如HABTM映射器表。
在您的情况下,它的问题是HABTM映射器表具有列article_id
,category_id
但是根据实现,它没有模型,因此seed_dump
会向db/seed.rb
#<Class:0x007f92d70e2268>.create!([
{article_id: 1, category_id: 1}
])
当您尝试运行时,它将不起作用rake db:seed
。
替代解决方案:
只需从您的服务器中提取数据库转储,然后通过将其导入到新计算机上的db服务器中来在新计算机上使用它即可。
如果您使用mysql
,则需要执行以下操作:
mysqldump -u USERNAME -p DATABASE_NAME > FILE_NAME.sql
它将要求您输入mysql密码。输入该内容,它将数据导出到FILE_NAME.sql
现在要在另一台机器上导入数据库,
mysql -u USERNAME -p DATABASE_NAME < FILE_NAME.sql
有关数据库导入/导出的更多信息
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句