2
votes

Is there a step that I missed, perhaps a "syncdb" step for Google Cloud SQL? I read both of these: Using Google Cloud SQL

Django Support

My application has access to Google Cloud SQL but I think I just have to create the tables in Google Cloud SQL. What's the command to do that? Maybe that will fix it. Running python manage.py syncdb updates SQL on localhost but I'd like to update the database on Google Cloud SQl.

EDIT: Below are the Google Cloud SQL settings and the settings.py for my project. Google Cloud SQL settingssettings.py

1
Could you show your settings.file and Google configuration? I think you have not set the right database. - Edwin Lunando
you have to edit your settings so that it uses the remote SQL db rather then the local one them syncdb will update the remote db - user2266449
Thanks! How should I change my current settings? I added them to the question. - Sticky

1 Answers

0
votes

There are three type of connections: 1) your dev machine to your local mysql instance (Easy!) 2) your app engine instance (in Google Cloud) to Google Cloud SQL instance (the same way described in the document) 3) your dev machine to cloudsql instance (Hard): the encouraged way is to obtain an static IP (for a while that doesn't cost you much money), and use the IP to connect cloudsql instance.

Django code [3] should allow you switch between above cases. The command [1] allows you to syncdb to cloudsql instance. If you want your django model to manipulate data in cloudsql instance, you can add config [2] to app.yaml

[1]

SETTING_MODE=prod ./manage.py syncdb

[2]

env_variables:
  SETTINGS_MODE: prod

[3]

import os
if os.getenv('SERVER_SOFTWARE', '').startswith('Google App Engine'):
    # Running on production App Engine, so use a Google Cloud SQL database.
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'HOST': '/cloudsql/your-project-id:your-instance-name',
            'NAME': 'django_test',
            'USER': 'root',
        }
    }
elif os.getenv('SETTINGS_MODE') == 'prod':
    # Running in development, but want to access the Google Cloud SQL instance
    # in production.
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'HOST': 'static-ip-of-cloudsql-instance',
            'PORT': '3306',
            'NAME': 'django_test',
            'USER': 'username',
            'PASSWORD': 'password'
        }
    }
else:
    # Running in development, so use a local MySQL database.
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            'NAME': 'django_test',
            'USER': 'root',
            'PASSWORD': 'root',
        }
    }