raiz
V2EX  ›  Heroku

求助: django 部署在 heroku , 数据库出问题, 想知道在 免费版的 heroku app 上 如何管理数据库

  •  1
     
  •   raiz · Jul 5, 2015 · 6889 views
    This topic created in 4019 days ago, the information mentioned may be changed or developed.
    1. 我开发环境是 python34 django 1.8.1
    2. 本地用数据库是 sqlite3, heroku 用到 postgres
    3. 我部署在heroku上是给另一个人测试用的,由于一直变更需求,表结构要变
    4. 我情景是,我之前的工程已经在 herok 上跑,没有问题, 然后我在 model中添加了一个 class Message(models.Model): user = models.ForeignKey('GameUser') 而 GameUser 是原来有的一个model
    5. 然后我push了,然后在heroku 上执行

    python manage.py makemigrations
    python manage.py migrate
    然后进入 python manage.py shell , 导入model后, 执行 Message.objects.all() ,提示上面的提问里错误

    Traceback (most recent call last):
    File "<console>", line 1, in <module>
    File "/app/.heroku/pythonb/python3.4/site-packages/django/models/query.py", line 138, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
    File "/app/.heroku/pythonb/python3.4/site-packages/django/models/query.py", line 162, in __iter__
    self._fetch_all()
    File "/app/.heroku/pythonb/python3.4/site-packages/django/models/query.py", line 965, in _fetch_all
    self._result_cache = list(self.iterator())
    File "/app/.heroku/pythonb/python3.4/site-packages/django/models/query.py", line 238, in iterator
    results = compiler.execute_sql()
    File "/app/.heroku/pythonb/python3.4/site-packages/django/modelsl/compiler.py", line 837, in execute_sql
    cursor.execute(sql, params)
    File "/app/.heroku/pythonb/python3.4/site-packages/django/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
    File "/app/.heroku/pythonb/python3.4/site-packages/django/utils.py", line 97, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
    File "/app/.heroku/pythonb/python3.4/site-packages/django/utils/six.py", line 658, in reraise
    raise value.with_traceback(tb)
    File "/app/.heroku/pythonb/python3.4/site-packages/django/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
    django.db.utils.ProgrammingError: relation "dbmanager_message" does not exist
    LINE 1: ..."msg_code", "dbmanager_message"."parameters" FROM "dbmanager...

    然而执行 GameUser.objects.all() 并不会报错
    4 replies    2015-07-06 00:11:38 +08:00
    fuermosi777
        1
    fuermosi777  
       Jul 5, 2015
    可以直接psql连到heroku app的数据库里添表
    raiz
        2
    raiz  
    OP
       Jul 5, 2015
    @fuermosi777 谢谢指点 是指本地下载 psql , 然后远程连接到 heroku的数据库吗? 我的表里很多字段,是不是要写很多sql语句?
    fuermosi777
        3
    fuermosi777  
       Jul 5, 2015
    我以前用1.6的时候 是psql直接连heroku数据库 然后本地运行一下`python manage.py sqlall app`,自动生成添加字段或者表的sql语句,然后复制到psql里运行一下就好了。好像自从Django1.8以后添加了migration功能。但由于模型不是经常修改,所以直接修改的方法并不是很麻烦。
    raiz
        4
    raiz  
    OP
       Jul 6, 2015
    @fuermosi777 最后采用了 本地使用 postgres, pg_dump 为 txt文件, 然使用 heroku 的pg:backup restore 为远程数据库
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3912 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 04:27 · PVG 12:27 · LAX 21:27 · JFK 00:27
    ♥ Do have faith in what you're doing.