简体   繁体   中英

How to solve "table "auth_permission" already exists" error when the database is shared among two Django projects

in this question I learned how to make two Django projects use the same database. I have:

projects
  project_1
    settings.py
    ...
  project_2
    settings.py
    ...

and

# project_1/settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(PROJECT_ROOT, 'development.db'),
    },
}


# project_2/settings.py

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(
            os.path.dirname(os.path.dirname(PROJECT_ROOT)),
            'project_1',
            'development.db'
        ),
    },
}

In project_2/ , when I run:

python manage.py syncdb

I get:

django.db.utils.OperationalError: table "auth_permission" already exists

I guess this happens because python fails in trying to add project_2 tables that already exists in the shared db.

How can I add to the shared db only those project_2 tables not already existing in the common database?

EDIT: After telling project_2/ to use project_1/ db, I run the syncdb and get the existing table error. I do not have a migration file. Shall I run a different command before syncing?

You can open the migrations file and comment out the SQL that tries to create the table. Then run migrations again.

(another possibility would be to delete the table in the database, but you'd lose the data in the table.)

If you use django version >1.7 you should use migrations ( Django Migrations - make sure to check version matches yours) Then you can make migrations more consistently (no conflicts). Even if they happen (but under very specific conditions), you can always '--fake' migrations on second host

Django 1.8.15 for project_2/. I've just checked project_1/ django version and it is 1.6. I was convinced that both projects where using the same django version.. Is this the main problem?

Yes. Because django 1.6 and django 1.8 use different syncdb commands. syncdb in 1.8 is migrate , so when you do syncdb in 1.8 you are applying migrations, not just creating tables. Use same django version and problem should be solved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM