migrate error

87 views
Skip to first unread message

Christoph Knapp

unread,
Oct 22, 2015, 11:40:18 AM10/22/15
to Django users
Hi,
I have a legacy database and used "python manage.py inspectdb > models.py" to create a models.py file. After I modified the file all errors went away when I makemigrations. This step works without problems on the only app I have in my project. When I go "python manage.py migrate" I get the following error.

Operations to perform:
  Synchronize unmigrated apps: staticfiles, messages
  Apply all migrations: admin, contenttypes, sessions, auth, readDatabase
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
  Installing custom SQL...
Running migrations:
  Rendering model states... DONE
  Applying readDatabase.0001_initial...Traceback (most recent call last):
  File "/home/christoph_knapp/Bioinformatik/variantendatenbank_tool/manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 351, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 343, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 394, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 445, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 222, in handle
    executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 110, in migrate
    self.apply_migration(states[migration], migration, fake=fake, fake_initial=fake_initial)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py", line 148, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/migration.py", line 115, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/usr/local/lib/python2.7/dist-packages/django/db/migrations/operations/models.py", line 59, in database_forwards
    schema_editor.create_model(model)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 286, in create_model
    self.execute(sql, params or None)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py", line 111, in execute
    cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 97, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 62, in execute
    return self.cursor.execute(sql)
django.db.utils.ProgrammingError: FEHLER:  Relation „auth_group“ existiert bereits

The last line contains a little bit of German which translates into "Relation "auth_group" already exists". The auth_group table was generated by inspectdb and is none of the tables from the legacy database. The only change I made at those tables were that I removed the line "managed=False". When I put this back in I get the same error. The python code for the auth_group table is below.

class AuthGroup(models.Model):
    name = models.CharField(unique=True, max_length=80)

    class Meta:
        db_table = 'auth_group'

I just started with django, so let me know whether you need anything else.

Regards

Christoph

Mike Dewhirst

unread,
Oct 22, 2015, 9:31:11 PM10/22/15
to django...@googlegroups.com
On 22/10/2015 10:40 PM, Christoph Knapp wrote:
> Hi,
> I have a legacy database and used "python manage.py inspectdb >
> models.py" to create a models.py file. After I modified the file all
> errors went away when I makemigrations. This step works without problems
> on the only app I have in my project. When I go "python manage.py
> migrate" I get the following error.
>
> Operations to perform:
> Â Synchronize unmigrated apps: staticfiles, messages
> Â Apply all migrations: admin, contenttypes, sessions, auth, readDatabase
> Synchronizing apps without migrations:
> Â Creating tables...
> Â Â Â Running deferred SQL...
> Â Installing custom SQL...
> Running migrations:
> Â Rendering model states... DONE
> Â Applying readDatabase.0001_initial...Traceback (most recent call last):
> Â File
> "/home/christoph_knapp/Bioinformatik/variantendatenbank_tool/manage.py",
> line 10, in <module>
> Â Â Â execute_from_command_line(sys.argv)
> Â File
> "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py",
> line 351, in execute_from_command_line
> Â Â Â utility.execute()
> Â File
> "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py",
> line 343, in execute
> Â Â Â self.fetch_command(subcommand).run_from_argv(self.argv)
> Â File
> "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py",
> line 394, in run_from_argv
> Â Â Â self.execute(*args, **cmd_options)
> Â File
> "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py",
> line 445, in execute
> Â Â Â output = self.handle(*args, **options)
> Â File
> "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py",
> line 222, in handle
> Â Â Â executor.migrate(targets, plan, fake=fake, fake_initial=fake_initial)
> Â File
> "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py",
> line 110, in migrate
> Â Â Â self.apply_migration(states[migration], migration, fake=fake,
> fake_initial=fake_initial)
> Â File
> "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py",
> line 148, in apply_migration
> Â Â Â state = migration.apply(state, schema_editor)
> Â File
> "/usr/local/lib/python2.7/dist-packages/django/db/migrations/migration.py",
> line 115, in apply
> Â Â Â operation.database_forwards(self.app_label, schema_editor,
> old_state, project_state)
> Â File
> "/usr/local/lib/python2.7/dist-packages/django/db/migrations/operations/models.py",
> line 59, in database_forwards
> Â Â Â schema_editor.create_model(model)
> Â File
> "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py",
> line 286, in create_model
> Â Â Â self.execute(sql, params or None)
> Â File
> "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py",
> line 111, in execute
> Â Â Â cursor.execute(sql, params)
> Â File
> "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py",
> line 79, in execute
> Â Â Â return super(CursorDebugWrapper, self).execute(sql, params)
> Â File
> "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py",
> line 64, in execute
> Â Â Â return self.cursor.execute(sql, params)
> Â File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py",
> line 97, in __exit__
> Â Â Â six.reraise(dj_exc_type, dj_exc_value, traceback)
> Â File
> "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py",
> line 62, in execute
> Â Â Â return self.cursor.execute(sql)
> django.db.utils.ProgrammingError: FEHLER: Relation „auth_group“
> existiert bereits
>
> The last line contains a little bit of German which translates into
> "Relation "auth_group" already exists". The auth_group table was
> generated by inspectdb and is none of the tables from the legacy
> database. The only change I made at those tables were that I removed the
> line "managed=False". When I put this back in I get the same error. The
> python code for the auth_group table is below.
>
> class AuthGroup(models.Model):
> Â Â Â name = models.CharField(unique=True, max_length=80)
>
> Â Â Â class Meta:
> Â Â Â Â Â Â Â db_table = 'auth_group'

auth_group is a Django table which you don't need to install. In your
models.py remove all models except your own, drop the database and try
again. The models/tables created for you by Django and the Admin are ...

auth_group
auth_group_permissions
auth_permission
auth_user
auth_user_groups
auth_user_user_permissions
django_admin_log
django_contenttype
django_migrations
django_session

Cheers

Mike

>
> I just started with django, so let me know whether you need anything else.
>
> Regards
>
> Christoph
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/a87703ec-cc25-4404-accf-e5cd800e23c7%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/a87703ec-cc25-4404-accf-e5cd800e23c7%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

Christoph Knapp

unread,
Oct 23, 2015, 5:23:30 AM10/23/15
to Django users
Ahh OK, the only explanation I have, is that someone already tried to use django on the legacy database and the djando tables were already there. The inspectdb tool put them in the models.py file which caused the error. I did what you said and it worked. I just looked at the documentation of the db and those tables were not in there.

Thanks

Christoph

Mike Dewhirst

unread,
Oct 24, 2015, 5:08:04 AM10/24/15
to django...@googlegroups.com
On 23/10/2015 4:23 PM, Christoph Knapp wrote:
> Ahh OK, the only explanation I have, is that someone already tried to
> use django on the legacy database and the djando tables were already
> there. The inspectdb tool put them in the models.py file which caused
> the error. I did what you said and it worked. I just looked at the
> documentation of the db and those tables were not in there.

Yes. Sounds reasonable. The other way it can happen is that the legacy
db was actually created by a Django project and both Admin and Auth were
installed apps.

>
> Thanks
>
> Christoph
>
>
> On Thursday, October 22, 2015 at 11:31:11 PM UTC+2, Mike Dewhirst wrote:
>
> On 22/10/2015 10:40 PM, Christoph Knapp wrote:
> > Hi,
> > I have a legacy database and used "python manage.py inspectdb >
> > models.py" to create a models.py file. After I modified the file all
> > errors went away when I makemigrations. This step works without
> problems
> > on the only app I have in my project. When I go "python manage.py
> > migrate" I get the following error.
> >
> > Operations to perform:
> > Â  Synchronize unmigrated apps: staticfiles, messages
> > Â  Apply all migrations: admin, contenttypes, sessions, auth,
> readDatabase
> > Synchronizing apps without migrations:
> > Â  Creating tables...
> > Â Â Â  Running deferred SQL...
> > Â  Installing custom SQL...
> > Running migrations:
> > Â  Rendering model states... DONE
> > Â  Applying readDatabase.0001_initial...Traceback (most recent
> call last):
> > Â  File
> >
> "/home/christoph_knapp/Bioinformatik/variantendatenbank_tool/manage.py",
>
> > line 10, in <module>
> > Â Â Â  execute_from_command_line(sys.argv)
> > Â  File
> >
> "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py",
>
> > line 351, in execute_from_command_line
> > Â Â Â  utility.execute()
> > Â  File
> >
> "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py",
>
> > line 343, in execute
> > Â Â Â  self.fetch_command(subcommand).run_from_argv(self.argv)
> > Â  File
> >
> "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py",
>
> > line 394, in run_from_argv
> > Â Â Â  self.execute(*args, **cmd_options)
> > Â  File
> >
> "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py",
>
> > line 445, in execute
> > Â Â Â  output = self.handle(*args, **options)
> > Â  File
> >
> "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py",
>
> > line 222, in handle
> > Â Â Â  executor.migrate(targets, plan, fake=fake,
> fake_initial=fake_initial)
> > Â  File
> >
> "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py",
>
> > line 110, in migrate
> > Â Â Â  self.apply_migration(states[migration], migration,
> fake=fake,
> > fake_initial=fake_initial)
> > Â  File
> >
> "/usr/local/lib/python2.7/dist-packages/django/db/migrations/executor.py",
>
> > line 148, in apply_migration
> > Â Â Â  state = migration.apply(state, schema_editor)
> > Â  File
> >
> "/usr/local/lib/python2.7/dist-packages/django/db/migrations/migration.py",
>
> > line 115, in apply
> > Â Â Â  operation.database_forwards(self.app_label,
> schema_editor,
> > old_state, project_state)
> > Â  File
> >
> "/usr/local/lib/python2.7/dist-packages/django/db/migrations/operations/models.py",
>
> > line 59, in database_forwards
> > Â Â Â  schema_editor.create_model(model)
> > Â  File
> >
> "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py",
>
> > line 286, in create_model
> > Â Â Â  self.execute(sql, params or None)
> > Â  File
> >
> "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/schema.py",
>
> > line 111, in execute
> > Â Â Â  cursor.execute(sql, params)
> > Â  File
> >
> "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py",
> > line 79, in execute
> > Â Â Â  return super(CursorDebugWrapper, self).execute(sql,
> params)
> > Â  File
> >
> "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py",
> > line 64, in execute
> > Â Â Â  return self.cursor.execute(sql, params)
> > Â  File
> "/usr/local/lib/python2.7/dist-packages/django/db/utils.py",
> > line 97, in __exit__
> > Â Â Â  six.reraise(dj_exc_type, dj_exc_value, traceback)
> > Â  File
> >
> "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py",
> > line 62, in execute
> > Â Â Â  return self.cursor.execute(sql)
> > django.db.utils.ProgrammingError: FEHLER:Â  Relation
> „auth_group“
> > existiert bereits
> >
> > The last line contains a little bit of German which translates into
> > "Relation "auth_group" already exists". The auth_group table was
> > generated by inspectdb and is none of the tables from the legacy
> > database. The only change I made at those tables were that I
> removed the
> > line "managed=False". When I put this back in I get the same
> error. The
> > python code for the auth_group table is below.
> >
> > class AuthGroup(models.Model):
> > Â Â Â  name = models.CharField(unique=True, max_length=80)
> >
> > Â Â Â  class Meta:
> > Â Â Â Â Â Â Â  db_table = 'auth_group'
> > an email to django-users...@googlegroups.com <javascript:>
> > <mailto:django-users...@googlegroups.com <javascript:>>.
> > To post to this group, send email to django...@googlegroups.com
> <javascript:>
> > <mailto:django...@googlegroups.com <javascript:>>.
> <http://groups.google.com/group/django-users>.
> <https://groups.google.com/d/msgid/django-users/a87703ec-cc25-4404-accf-e5cd800e23c7%40googlegroups.com?utm_medium=email&utm_source=footer
> <https://groups.google.com/d/optout>.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/0a9e93b8-8193-4b2d-8069-47bb06ce2641%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/0a9e93b8-8193-4b2d-8069-47bb06ce2641%40googlegroups.com?utm_medium=email&utm_source=footer>.
Reply all
Reply to author
Forward
0 new messages