【问题标题】:django migrations in postgresql fails (Even default migrations but works in sqlite and mysql)postgresql 中的 django 迁移失败(即使是默认迁移,但适用于 sqlite 和 mysql)
【发布时间】:2018-06-27 03:03:02
【问题描述】:

我创建了一个新的 django 项目,并在 settings.py 中添加了以下数据库设置

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'postgres',
        'USER': 'postgres',
        'PASSWORD': 'root',
        'HOST': '127.0.0.1',
        'PORT': '5432',
        'OPTIONS':{'options': '-c search_path=resttest'}
    }
}

我刚刚运行了“python manage.py migrate”

但是这个错误

django.db.utils.ProgrammingError: constraint "django_admin_log_user_id_c564eba6_fk" does not exist

当尝试使用默认 sqlite 或 MySQL 更改数据库设置时,它工作正常。 我什至尝试使用'ENGINE': 'django.db.backends.postgresql',如文档中所示。 谁能帮我猜猜是什么问题?

【问题讨论】:

    标签: django postgresql migrate


    【解决方案1】:
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': 'your_custom_database_name',
            'USER': 'postgres',
            'PASSWORD': 'root',
            'HOST': '127.0.0.1',
            'PORT': '5432',
            'OPTIONS':{'options': '-c search_path=resttest'}
        }
    }
    

    问题是您正在访问 postgres 数据库的默认数据库,创建另一个具有不同名称的数据库并迁移。

    【讨论】:

    • 谢谢!。当我从默认数据库更改为自定义数据库时它起作用了
    • 很高兴知道,如果答案有帮助,别忘了接受答案
    【解决方案2】:

    如果您使用的是 Linux,请执行以下操作。安装 PostgreSQL 及其所需的软件包。

    sudo apt-get install postgresql postgresql-contrib
    

    以 postgres 用户身份登录,默认情况下只有该用户有权在 PostgreSQL 中创建数据库。

    sudo su - postgres
    

    现在,您以 postgres 用户身份登录。创建数据库用户并为其分配必要的权限。

    postgres@ubuntu:~$ createuser --interactive -P
    Enter name of role to add: db_user
    Enter password for new role: 
    Enter it again: 
    Shall the new role be a superuser? (y/n) n
    Shall the new role be allowed to create databases? (y/n) n
    Shall the new role be allowed to create more new roles? (y/n) n
    postgres@ubuntu:~$
    

    在上面的例子中,数据库用户名是 db_user。您可以根据自己的意愿命名。现在创建数据库。根据您的 Django 应用程序,也给数据库起一个适当的名称。

    postgres@ubuntu:~$ createdb --owner db_user django_db
    

    这里 db_user 是数据库用户,django_db 是数据库名称。现在从 postgres 用户注销。

    postgres@ubuntu:~$ logout
    

    现在,我们需要配置 PostgreSQL 以便它可以与 Django 应用程序通信。为此,请安装 psycopg2 数据库适配器。但是这个适配器有一些包依赖,所以先安装它们。

    sudo apt-get install libpq-dev python3-dev
    

    现在安装,

    pip install psycopg2
    

    现在,在项目的 settings.py 文件中配置数据库部分。

    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.postgresql_psycopg2',
            'NAME': 'django_db',
            'USER': 'db_user',
            'PASSWORD': '<password you entered when creating db_user>',
            'HOST': 'localhost',
            'PORT': '',                      # Set to empty string for default.
        }
    }
    

    现在,迁移数据库。

    python manage.py migrate
    

    我希望这会有所帮助。

    最好的,

    达瓦尔

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-01-14
      • 2012-06-10
      • 2020-05-29
      • 2016-04-27
      • 2017-03-10
      • 2016-11-08
      • 2018-10-13
      相关资源
      最近更新 更多