I am making a Rails API using jbuilder to serve as my database for a React app I'm building. I am using PostgreSQL. I can get the index.json.jbuilder view to work on my local server:
我正在使用jbuilder创建一个Rails API作为我正在构建的React应用程序的数据库。我正在使用PostgreSQL。我可以让index.json.jbuilder视图在我的本地服务器上运行:
{
"owners": [
{
"first_name": "Alex",
"last_name": "Hardy",
"revenue": 100000,
"audit_score": 89,
"passing": true,
}, ...........
{
But these instances are not going to Heroku's server. I have created the heroku app, pushed my app to heroku (git push heroku master) , and migrated the database with:
但是这些实例不会进入Heroku的服务器。我创建了heroku应用程序,将我的应用程序推送到heroku(git push heroku master),并使用以下命令迁移数据库:
heroku run rake db:migrate
It has stored the tables because when I run:
它存储了表,因为我运行时:
Heroku pg:info
I get:
我明白了:
Plan: Hobby-dev
Status: Available
Connections: 2/20
PG Version: 9.6.2
Created: 2017-07-26 19:09 UTC
Data Size: 7.4 MB
Tables: 3
Rows: 2/10000 (In compliance)
Fork/Follow: Unsupported
Rollback: Unsupported
Add-on: postgresql-contoured-63642
If I
如果我
heroku console
and check for the table Owner (Owner.new) it shows up with the correct properties ("first_name", "last_name"....). BUT, when I go to the site: https://peaceful-basin-47322.herokuapp.com/owners it only shows the table, with none of the instances of that model:
并检查表所有者(Owner.new)它是否显示正确的属性(“first_name”,“last_name”....)。但是,当我访问该网站时:https://peaceful-basin-47322.herokuapp.com/owners它只显示该表,没有该模型的任何实例:
{
"owners": []
}
How to I migrate the instances of my model to Heroku?
如何将我的模型实例迁移到Heroku?
2 个解决方案
#1
1
the data from your localhost is Different, with the data in your heroku. if you want it to be same you need dump your data from your localhost then import it to your heroku,
来自localhost的数据与您的heroku中的数据不同。如果你想要它是相同的,你需要从localhost转储数据然后导入到你的heroku,
first dump your postgres localhost data
首先转储postgres localhost数据
PGPASSWORD=mypassword pg_dump -Fc --no-acl --no-owner -h localhost -U myuser mydb > mydb.dump
then upload it to internet where heroku can download it, like amazon s3
然后上传到互联网,heroku可以下载它,就像亚马逊s3
heroku pg:backups:restore 'https://s3.amazonaws.com/me/items/3H0q/mydb.dump' DATABASE_URL
more you can read here https://devcenter.heroku.com/articles/heroku-postgres-import-export
更多你可以在这里阅读https://devcenter.heroku.com/articles/heroku-postgres-import-export
rake db:migrate
only create the database tables, without the data, if you want to also create the data you can make it from seeds.rb
and then do rake db:seed
rake db:migrate只创建没有数据的数据库表,如果你还要创建数据,你可以从seeds.rb创建它,然后做rake db:seed
#2
0
I think the easiest way if you just want to populate the DB in heroku with your local DB contents is to do, on your terminal, from your app folder:
我认为最简单的方法是,如果你只想用你的本地数据库内容填充heroku中的数据库,就可以在终端上从你的app文件夹中执行:
heroku pg:info
# this will show you something like:
# === HEROKU_POSTGRESQL_RED
# Plan Standard 0
# ...
# then with this you have the DB name HEROKU_POSTGRESQL_RED, do
heroku pg:push your_local_db_name_here HEROKU_POSTGRESQL_RED --app change_to_your_app_name
If your database is not empty it will ask you to do pg:reset
first. Remember you need a clean database to do this, if you want to mix your records from local into an existing DB then you need to do it in other ways.
如果您的数据库不为空,它会要求您先执行pg:reset。请记住,如果要将本地记录与现有数据库混合,则需要一个干净的数据库来执行此操作,然后您需要以其他方式执行此操作。
Source: https://devcenter.heroku.com/articles/heroku-postgresql#pg-push-and-pg-pull
资料来源:https://devcenter.heroku.com/articles/heroku-postgresql#pg-push-and-pg-pull
#1
1
the data from your localhost is Different, with the data in your heroku. if you want it to be same you need dump your data from your localhost then import it to your heroku,
来自localhost的数据与您的heroku中的数据不同。如果你想要它是相同的,你需要从localhost转储数据然后导入到你的heroku,
first dump your postgres localhost data
首先转储postgres localhost数据
PGPASSWORD=mypassword pg_dump -Fc --no-acl --no-owner -h localhost -U myuser mydb > mydb.dump
then upload it to internet where heroku can download it, like amazon s3
然后上传到互联网,heroku可以下载它,就像亚马逊s3
heroku pg:backups:restore 'https://s3.amazonaws.com/me/items/3H0q/mydb.dump' DATABASE_URL
more you can read here https://devcenter.heroku.com/articles/heroku-postgres-import-export
更多你可以在这里阅读https://devcenter.heroku.com/articles/heroku-postgres-import-export
rake db:migrate
only create the database tables, without the data, if you want to also create the data you can make it from seeds.rb
and then do rake db:seed
rake db:migrate只创建没有数据的数据库表,如果你还要创建数据,你可以从seeds.rb创建它,然后做rake db:seed
#2
0
I think the easiest way if you just want to populate the DB in heroku with your local DB contents is to do, on your terminal, from your app folder:
我认为最简单的方法是,如果你只想用你的本地数据库内容填充heroku中的数据库,就可以在终端上从你的app文件夹中执行:
heroku pg:info
# this will show you something like:
# === HEROKU_POSTGRESQL_RED
# Plan Standard 0
# ...
# then with this you have the DB name HEROKU_POSTGRESQL_RED, do
heroku pg:push your_local_db_name_here HEROKU_POSTGRESQL_RED --app change_to_your_app_name
If your database is not empty it will ask you to do pg:reset
first. Remember you need a clean database to do this, if you want to mix your records from local into an existing DB then you need to do it in other ways.
如果您的数据库不为空,它会要求您先执行pg:reset。请记住,如果要将本地记录与现有数据库混合,则需要一个干净的数据库来执行此操作,然后您需要以其他方式执行此操作。
Source: https://devcenter.heroku.com/articles/heroku-postgresql#pg-push-and-pg-pull
资料来源:https://devcenter.heroku.com/articles/heroku-postgresql#pg-push-and-pg-pull