Google Cloud SQL:运行错误" python manage.py makemigrations"

时间:2017-09-29 13:57:35

标签: python django python-2.7 google-cloud-platform google-cloud-sql

我正在尝试运行python manage.py makemigrations,而此命令在另一个终端窗口上运行

./cloud_sql_proxy -instances="test-project-181410:us-central1:poll-instance"=tcp:5432

运行后

python manage.py makemigrtions

在给我回复之前需要一段时间。但是,我收到了错误

  

将图像更改为代码

> Traceback (most recent call last):   File "manage.py", line 24, in
> <module>
>     execute_from_command_line(sys.argv)   File "/Users/macintosh/Documents/Online_Job_Switch/cloud-test-project/python-docs-samples/appengine/flexible/django_cloudsql/env/lib/python2.7/site-packages/django/core/management/__init__.py",
> line 364, in execute_from_command_line
>     utility.execute()   File "/Users/macintosh/Documents/Online_Job_Switch/cloud-test-project/python-docs-samples/appengine/flexible/django_cloudsql/env/lib/python2.7/site-packages/django/core/management/__init__.py",
> line 356, in execute
>     self.fetch_command(subcommand).run_from_argv(self.argv)   File "/Users/macintosh/Documents/Online_Job_Switch/cloud-test-project/python-docs-samples/appengine/flexible/django_cloudsql/env/lib/python2.7/site-packages/django/core/management/base.py",
> line 283, in run_from_argv
>     self.execute(*args, **cmd_options)   File "/Users/macintosh/Documents/Online_Job_Switch/cloud-test-project/python-docs-samples/appengine/flexible/django_cloudsql/env/lib/python2.7/site-packages/django/core/management/base.py",
> line 330, in execute
>     output = self.handle(*args, **options)   File "/Users/macintosh/Documents/Online_Job_Switch/cloud-test-project/python-docs-samples/appengine/flexible/django_cloudsql/env/lib/python2.7/site-packages/django/core/management/commands/makemigrations.py",
> line 110, in handle
>     loader.check_consistent_history(connection)   File "/Users/macintosh/Documents/Online_Job_Switch/cloud-test-project/python-docs-samples/appengine/flexible/django_cloudsql/env/lib/python2.7/site-packages/django/db/migrations/loader.py",
> line 282, in check_consistent_history
>     applied = recorder.applied_migrations()   File "/Users/macintosh/Documents/Online_Job_Switch/cloud-test-project/python-docs-samples/appengine/flexible/django_cloudsql/env/lib/python2.7/site-packages/django/db/migrations/recorder.py",
> line 65, in applied_migrations
>     self.ensure_schema()   File "/Users/macintosh/Documents/Online_Job_Switch/cloud-test-project/python-docs-samples/appengine/flexible/django_cloudsql/env/lib/python2.7/site-packages/django/db/migrations/recorder.py",
> line 52, in ensure_schema
>     if self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor()):  
> File
> "/Users/macintosh/Documents/Online_Job_Switch/cloud-test-project/python-docs-samples/appengine/flexible/django_cloudsql/env/lib/python2.7/site-packages/django/db/backends/base/base.py",
> line 254, in cursor
>     return self._cursor()   File "/Users/macintosh/Documents/Online_Job_Switch/cloud-test-project/python-docs-samples/appengine/flexible/django_cloudsql/env/lib/python2.7/site-packages/django/db/backends/base/base.py",
> line 229, in _cursor
>     self.ensure_connection()   File "/Users/macintosh/Documents/Online_Job_Switch/cloud-test-project/python-docs-samples/appengine/flexible/django_cloudsql/env/lib/python2.7/site-packages/django/db/backends/base/base.py",
> line 213, in ensure_connection
>     self.connect()   File "/Users/macintosh/Documents/Online_Job_Switch/cloud-test-project/python-docs-samples/appengine/flexible/django_cloudsql/env/lib/python2.7/site-packages/django/db/utils.py",
> line 94, in __exit__
>     six.reraise(dj_exc_type, dj_exc_value, traceback)   File "/Users/macintosh/Documents/Online_Job_Switch/cloud-test-project/python-docs-samples/appengine/flexible/django_cloudsql/env/lib/python2.7/site-packages/django/db/backends/base/base.py",
> line 213, in ensure_connection
>     self.connect()   File "/Users/macintosh/Documents/Online_Job_Switch/cloud-test-project/python-docs-samples/appengine/flexible/django_cloudsql/env/lib/python2.7/site-packages/django/db/backends/base/base.py",
> line 189, in connect
>     self.connection = self.get_new_connection(conn_params)   File "/Users/macintosh/Documents/Online_Job_Switch/cloud-test-project/python-docs-samples/appengine/flexible/django_cloudsql/env/lib/python2.7/site-packages/django/db/backends/postgresql/base.py",
> line 176, in get_new_connection
>     connection = Database.connect(**conn_params)   File "/Users/macintosh/Documents/Online_Job_Switch/cloud-test-project/python-docs-samples/appengine/flexible/django_cloudsql/env/lib/python2.7/site-packages/psycopg2/__init__.py",
> line 130, in connect
>     conn = _connect(dsn, connection_factory=connection_factory, **kwasync) django.db.utils.OperationalError: server closed the connection unexpectedly    This probably means the server terminated
> abnormally    before or while processing the request.

这是我在settings.py中的代码

  

将图像更改为代码

# Copyright 2015 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.8/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '----------------------------------------------'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

# SECURITY WARNING: App Engine's security features ensure that it is safe to
# have ALLOWED_HOSTS = ['*'] when the app is deployed. If you deploy a Django
# app not on App Engine, make sure to set an appropriate host here.
# See https://docs.djangoproject.com/en/1.10/ref/settings/
ALLOWED_HOSTS = ['*']

# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'polls'
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'django.middleware.security.SecurityMiddleware',
)

ROOT_URLCONF = 'mysite.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'mysite.wsgi.application'

# Database
# https://docs.djangoproject.com/en/1.8/ref/settings/#databases

# [START dbconfig]
DATABASES = {
    'default': {
        # If you are using Cloud SQL for MySQL rather than PostgreSQL, set
        # 'ENGINE': 'django.db.backends.mysql' instead of the following.
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'poll_db',
        'USER': 'rej956',
        'PASSWORD': '123456',
        # For MySQL, set 'PORT': '3306' instead of the following. Any Cloud
        # SQL Proxy instances running locally must also be set to tcp:3306.
        'PORT': '5432',
    }
}
# In the flexible environment, you connect to CloudSQL using a unix socket.
# Locally, you can use the CloudSQL proxy to proxy a localhost connection
# to the instance
DATABASES['default']['HOST'] = '/cloudsql/test-project-181410:us-central1:poll-instance'
if os.getenv('GAE_INSTANCE'):
    pass
else:
    DATABASES['default']['HOST'] = '127.0.0.1'
# [END dbconfig]

# Internationalization
# https://docs.djangoproject.com/en/1.8/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/

# [START staticurl]
# Fill in your cloud bucket and switch which one of the following 2 lines
# is commented to serve static content from GCS
# STATIC_URL = 'https://storage.googleapis.com/<your-gcs-bucket>/static/'
STATIC_URL = '/static/'
# [END staticurl]

STATIC_ROOT = 'static/'

非常感谢您的帮助。谢谢

0 个答案:

没有答案