Programming

Django South-테이블이 이미 존재합니다

procodes 2020. 5. 14. 21:21
반응형

Django South-테이블이 이미 존재합니다


South를 시작하려고합니다. 기존 데이터베이스가 있고 South ( syncdb, schemamigration --initial)를 추가했습니다 .

그런 다음 models.py필드를 추가하고 실행했습니다 ./manage.py schemamigration myapp --auto. 필드를 찾은 것 같았고 이것을 적용 할 수 있다고 말했습니다 ./manage.py migrate myapp. 그러나 그렇게하면 오류가 발생했습니다.

django.db.utils.DatabaseError: table "myapp_tablename" already exists

tablename에 나열된 첫 번째 테이블입니다 models.py.

Django 1.2, South 0.7을 실행 중입니다.


데이터베이스에 이미 테이블이 생성되었으므로 초기 마이그레이션을 가짜로 실행하면됩니다.

./manage.py migrate myapp --fake

모델의 스키마가 데이터베이스의 테이블 스키마와 동일한 지 확인하십시오.


./manage.py migrate myapp --fake를 수행 한 후에 "myapp_tablename"테이블에 이미 오류 중지가 발생하지만 DatabaseError는 myapp_mymodel.added_field 열을 표시하지 않습니다.

정확히 같은 문제가 발생했습니다!

1. 먼저이 문제를 일으키는 마이그레이션 번호확인하십시오 . 0010이라고 가정합니다.

2. 필요한 사항 :

./manage.py schemamigration myapp --add-field MyModel.added_field
./manage.py migrate myapp

둘 이상의 필드가 누락 된 경우 각 필드에 대해 반복해야합니다.

3. 이제 여러 가지 새로운 마이그레이션을 수행하므로 myapp / migrations에서 파일제거하십시오 (여러 필드를 추가해야하는 경우 추가로 0011).

4. 이것을 실행하십시오 :

./manage.py migrate myapp 0010

이제 ./manage.py 마이그레이션 myapp를 시도하십시오.

실패하지 않으면 준비가 된 것입니다. 누락 된 필드가 없는지 다시 확인하십시오.

편집하다:

South를 설치하는 프로덕션 데이터베이스가 있고 다른 환경에서 작성된 첫 번째 초기 마이그레이션이 이미 DB에있는 것과 동일한 경우이 문제가 발생할 수 있습니다. 해결책은 여기에서 훨씬 쉽습니다.

  1. 첫 번째 마이그레이션을 가짜 :

    ./manage migration myapp 0001-가짜

  2. 나머지 마이그레이션으로 롤 :

    ./manage 마이그레이션 myapp


이 오류가 발생했을 때 다른 원인이있었습니다.

필자의 경우 South는 어떻게 든 _remake_table () 에서 사용되는 임시 빈 테이블을 DB에 남겨 두었습니다 . 아마도 내가하지 말아야 할 방식으로 이주를 중단했을 것입니다. 어쨌든 _remake_table ()을 호출 할 때 이후의 각 새 마이그레이션 이미 존재하고 존재 하지sqlite3.pypysqlite2.dbapi2.OperationalError: table "_south_new_myapp_mymodel" already exists 않았기 때문에 오류를 발생시킵니다 .

_south_new 비트가 나에게 이상해 보였으므로 DB를 탐색하고 테이블을 보았고 _south_new_myapp_mymodel머리를 긁 었고 South의 소스를 보았고 그것이 정크라고 결정하고 테이블을 삭제했으며 모든 것이 잘되었습니다.


Perform these steps in order may help you:

1) python manage.py schemamigration apps.appname-초기

위의 단계는 기본적으로 마이그레이션 폴더를 만듭니다.

2) python manage.py migrate apps.appname --fake

가짜 마이그레이션을 생성합니다.

3) python manage.py schemamigration apps.appname-자동

그런 다음 원하는대로 필드를 추가하고 위 명령을 수행 할 수 있습니다.

4) python manage.py 마이그레이션 apps.appname


@pielgrzym과 같이 데이터베이스와 일치하지 않는 모델에 문제가 있고 최신 models.py 파일과 일치하도록 데이터베이스를 자동으로 마이그레이션하려는 경우 (및 동안 조명기에서 다시 작성하지 않는 모든 데이터를 지우십시오 migrate) :

manage.py schemamigration myapp --initial
manage.py migrate myapp --fake
manage.py migrate myapp zero
manage.py migrate myapp

이것은 단지 삭제하고 최신에 존재하는 재 작성 데이터베이스 테이블 것이다 models.py당신은 이전에서 데이터베이스에 쓰레기 테이블을 가질 수 있도록 파일 syncdbs 또는 migrateS. 이를 제거하려면 다음과 같이 모든 마이그레이션을 수행하십시오.

manage.py sqlclear myapp | manage.py sqlshell

And if that still leaves some CRUFT lying around in your database then you'll have to do an inspectdb and create the models.py file from that (for the tables and app that you want to clear) before doing the sqlclear and then restore your original models.py before creating the --initial migration and migrating to it. All this to avoid messing around with the particular flavor of SQL that your database needs.


If you have an existing database and app you can use the south conversion command

./manage.py convert_to_south myapp

This has to be applied before you do any changes to what is already in the database.

The convert_to_south command only works entirely on the first machine you run it on. Once you’ve committed the initial migrations it made into your VCS, you’ll have to run ./manage.py migrate myapp 0001 --fake on every machine that has a copy of the codebase (make sure they were up-to-date with models and schema first). ref: http://south.readthedocs.org/en/latest/convertinganapp.html


As temporary solution, you can comment the Table creation in the migration script.

class Migration(migrations.Migration):

    dependencies = [
        (...)
    ]

    operations = [
        #migrations.CreateModel(
        #    name='TABLE',
        #    fields=[
        #            ....
        #            ....
        #    ],
        #),
        ....
        ....

Or

If the existing table contains no rows (empty), then consider deleting the table like below. (This fix is recommended only if the table contains no rows). Also make sure this operation before the createModel operation.

class Migration(migrations.Migration):

    dependencies = [
        (...),
    ]

    operations = [
        migrations.RunSQL("DROP TABLE myapp_tablename;")
    ]

One more solution(maybe a temporary solution).

$ python manage.py sqlmigrate APP_NAME MIGRATION_NAME

eg.,.

$ python manage.py sqlmigrate users 0029_auto_20170310_1117

This will list all the migrations in raw sql queries. You can pick the queries which you want to run avoiding the part which creates the existing table

참고URL : https://stackoverflow.com/questions/3090648/django-south-table-already-exists

반응형