Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrations not working - Thinks table exists that does not #26

Closed
bijenkins opened this issue Oct 23, 2015 · 9 comments
Closed

Migrations not working - Thinks table exists that does not #26

bijenkins opened this issue Oct 23, 2015 · 9 comments

Comments

@bijenkins
Copy link

Getting the below error when upgrading from 2.02 to Current stable. Funny thing is, I've searched the table and there is NO dbmail_mailsubscription in the DB. So I don't know why this migration think's it's there.

RHEL 6.6 Python 2.7.10

(site)[bijenkins@bmositedev siteAppsite]$ python manage.py makemigrations --merge
Merging dbmail
  Branch 0002_auto_20151013_1509
    - Alter field message on mailtemplate
  Branch 0007_auto_20150708_2016
    - Alter field email on mailfromemail
    - Alter field email on mailgroupemail
    - Alter field email on maillogemail
    - Alter field context_note on mailtemplate
    - Alter field from_email on mailtemplate
    - Alter field is_html on mailtemplate
    - Add field backend to maillog
    - Add field provider to maillog
    - Alter field backend on maillog
    - Create model MailBaseTemplate
    - Add field base to mailtemplate
    - Create model MailSubscription
    - Change Meta options on mailsubscription
    - Alter field address on mailsubscription
    - Alter field backend on mailsubscription
    - Alter field defer_at_allowed_hours on mailsubscription
    - Alter field end_hour on mailsubscription
    - Alter field is_checked on mailsubscription
    - Alter field is_enabled on mailsubscription
    - Alter field start_hour on mailsubscription

Merging will only work if the operations printed above do not conflict
with each other (working on different fields or models)
Do you want to merge these migration branches? [y/N] y

Created new merge migration /home/site/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/dbmail/migrations/0008_merge.py
(site)[bijenkins@bmositedev siteAppsite]$ 
(site)[bijenkins@bmositedev siteAppsite]$ python manage.py migrate
DEBUG (0.000) SET SQL_AUTO_IS_NULL = 0; args=None
DEBUG (0.001) SHOW TABLES; args=None
DEBUG (0.001) SELECT `django_migrations`.`app`, `django_migrations`.`name` FROM `django_migrations`; args=()
Operations to perform:
  Synchronize unmigrated apps: allauth, app, tinymce, django_rq, suit
  Apply all migrations: account, explorer, thunderdomeV2, sessions, admin, documentation, sites, auth, thunderdome, contenttypes, dbmail, servicepathtable, socialaccount
Synchronizing apps without migrations:
DEBUG (0.001) SHOW TABLES; args=None
  Creating tables...
  Installing custom SQL...
  Installing indexes...
DEBUG (0.000) SET foreign_key_checks=0; args=None
DEBUG (0.000) SET foreign_key_checks=1; args=None
DEBUG (0.000) SET SQL_AUTO_IS_NULL = 0; args=None
DEBUG (0.000) SET foreign_key_checks=0; args=None
DEBUG (0.000) SET foreign_key_checks=1; args=None
DEBUG (0.000) SET SQL_AUTO_IS_NULL = 0; args=None
DEBUG (0.000) SET foreign_key_checks=0; args=None
DEBUG (0.000) SET foreign_key_checks=1; args=None
DEBUG (0.000) SET SQL_AUTO_IS_NULL = 0; args=None
DEBUG (0.000) SET foreign_key_checks=0; args=None
DEBUG (0.000) SET foreign_key_checks=1; args=None
DEBUG (0.000) SET SQL_AUTO_IS_NULL = 0; args=None
DEBUG (0.000) SET foreign_key_checks=0; args=None
DEBUG (0.000) SET foreign_key_checks=1; args=None
Running migrations:
  Applying dbmail.0002_auto_20150321_1539...DEBUG (0.000) SET SQL_AUTO_IS_NULL = 0; args=None
DEBUG (0.001) 
            SELECT kc.`constraint_name`, kc.`column_name`,
                kc.`referenced_table_name`, kc.`referenced_column_name`
            FROM information_schema.key_column_usage AS kc
            WHERE
                kc.table_schema = 'siteV2' AND
                kc.table_name = 'dbmail_mailtemplate'
        ; args=['siteV2', u'dbmail_mailtemplate']
DEBUG (0.001) 
            SELECT c.constraint_name, c.constraint_type
            FROM information_schema.table_constraints AS c
            WHERE
                c.table_schema = 'siteV2' AND
                c.table_name = 'dbmail_mailtemplate'
        ; args=['siteV2', u'dbmail_mailtemplate']
DEBUG (0.001) SHOW INDEX FROM `dbmail_mailtemplate`; args=None
DEBUG (0.001) SHOW TABLES; args=None
DEBUG (0.001) INSERT INTO `django_migrations` (`app`, `name`, `applied`) VALUES ('dbmail', '0002_auto_20150321_1539', '2015-10-23 00:56:52'); args=['dbmail', u'0002_auto_20150321_1539', u'2015-10-23 00:56:52']
 OK
  Applying dbmail.0003_maillog_backend...DEBUG ALTER TABLE `dbmail_maillog` ADD COLUMN `backend` varchar(25) DEFAULT %s NOT NULL; (params [u'mail'])
DEBUG (0.013) ALTER TABLE `dbmail_maillog` ADD COLUMN `backend` varchar(25) DEFAULT 'mail' NOT NULL; args=[u'mail']
DEBUG ALTER TABLE `dbmail_maillog` ALTER COLUMN `backend` DROP DEFAULT; (params [])
DEBUG (0.003) ALTER TABLE `dbmail_maillog` ALTER COLUMN `backend` DROP DEFAULT; args=[]
DEBUG CREATE INDEX `dbmail_maillog_b43fdd98` ON `dbmail_maillog` (`backend`); (params [])
DEBUG (0.011) CREATE INDEX `dbmail_maillog_b43fdd98` ON `dbmail_maillog` (`backend`); args=[]
DEBUG (0.001) SHOW TABLES; args=None
DEBUG (0.000) INSERT INTO `django_migrations` (`app`, `name`, `applied`) VALUES ('dbmail', '0003_maillog_backend', '2015-10-23 00:56:52'); args=['dbmail', u'0003_maillog_backend', u'2015-10-23 00:56:52']
 OK
  Applying dbmail.0004_auto_20150321_2214...DEBUG ALTER TABLE `dbmail_maillog` ADD COLUMN `provider` varchar(250) NULL; (params [])
DEBUG (0.014) ALTER TABLE `dbmail_maillog` ADD COLUMN `provider` varchar(250) NULL; args=[]
DEBUG CREATE INDEX `dbmail_maillog_9e9f3d70` ON `dbmail_maillog` (`provider`); (params [])
DEBUG (0.013) CREATE INDEX `dbmail_maillog_9e9f3d70` ON `dbmail_maillog` (`provider`); args=[]
DEBUG (0.001) SHOW TABLES; args=None
DEBUG (0.000) INSERT INTO `django_migrations` (`app`, `name`, `applied`) VALUES ('dbmail', '0004_auto_20150321_2214', '2015-10-23 00:56:52'); args=['dbmail', u'0004_auto_20150321_2214', u'2015-10-23 00:56:52']
 OK
  Applying dbmail.0005_auto_20150506_2201...DEBUG CREATE TABLE `dbmail_mailbasetemplate` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `name` varchar(100) NOT NULL UNIQUE, `message` longtext NOT NULL, `created` datetime NOT NULL, `updated` datetime NOT NULL); (params [])
DEBUG (0.005) CREATE TABLE `dbmail_mailbasetemplate` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `name` varchar(100) NOT NULL UNIQUE, `message` longtext NOT NULL, `created` datetime NOT NULL, `updated` datetime NOT NULL); args=[]
DEBUG ALTER TABLE `dbmail_mailtemplate` ADD COLUMN `base_id` integer NULL; (params [])
DEBUG (0.004) ALTER TABLE `dbmail_mailtemplate` ADD COLUMN `base_id` integer NULL; args=[]
DEBUG ALTER TABLE `dbmail_mailtemplate` ALTER COLUMN `base_id` DROP DEFAULT; (params [])
DEBUG (0.003) ALTER TABLE `dbmail_mailtemplate` ALTER COLUMN `base_id` DROP DEFAULT; args=[]
DEBUG CREATE INDEX `dbmail_mailtemplate_078dce83` ON `dbmail_mailtemplate` (`base_id`); (params [])
DEBUG (0.004) CREATE INDEX `dbmail_mailtemplate_078dce83` ON `dbmail_mailtemplate` (`base_id`); args=[]
DEBUG ALTER TABLE `dbmail_mailtemplate` ADD CONSTRAINT `dbmail_ma_base_id_6971ccd5fcd1b12f_fk_dbmail_mailbasetemplate_id` FOREIGN KEY (`base_id`) REFERENCES `dbmail_mailbasetemplate` (`id`); (params [])
DEBUG (0.004) ALTER TABLE `dbmail_mailtemplate` ADD CONSTRAINT `dbmail_ma_base_id_6971ccd5fcd1b12f_fk_dbmail_mailbasetemplate_id` FOREIGN KEY (`base_id`) REFERENCES `dbmail_mailbasetemplate` (`id`); args=[]
DEBUG (0.001) SHOW TABLES; args=None
DEBUG (0.000) INSERT INTO `django_migrations` (`app`, `name`, `applied`) VALUES ('dbmail', '0005_auto_20150506_2201', '2015-10-23 00:56:53'); args=['dbmail', u'0005_auto_20150506_2201', u'2015-10-23 00:56:53']
 OK
  Applying dbmail.0006_auto_20150708_0714...DEBUG CREATE TABLE `dbmail_mailsubscription` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `backend` varchar(50) NOT NULL, `start_hour` varchar(5) NOT NULL, `end_hour` varchar(5) NOT NULL, `is_enabled` bool NOT NULL, `is_checked` bool NOT NULL, `defer_at_allowed_hours` bool NOT NULL, `address` varchar(60) NOT NULL, `user_id` integer NULL); (params [])
DEBUG (0.005) CREATE TABLE `dbmail_mailsubscription` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `backend` varchar(50) NOT NULL, `start_hour` varchar(5) NOT NULL, `end_hour` varchar(5) NOT NULL, `is_enabled` bool NOT NULL, `is_checked` bool NOT NULL, `defer_at_allowed_hours` bool NOT NULL, `address` varchar(60) NOT NULL, `user_id` integer NULL); args=[]
DEBUG ALTER TABLE `dbmail_mailsubscription` ADD CONSTRAINT `dbmail_mailsubscription_user_id_440cd6feeb7e77b0_fk_auth_user_id` FOREIGN KEY (`user_id`) REFERENCES `auth_user` (`id`); (params [])
DEBUG (0.018) ALTER TABLE `dbmail_mailsubscription` ADD CONSTRAINT `dbmail_mailsubscription_user_id_440cd6feeb7e77b0_fk_auth_user_id` FOREIGN KEY (`user_id`) REFERENCES `auth_user` (`id`); args=[]
Traceback (most recent call last):
  File "manage.py", line 17, in <module>
    execute_from_command_line(sys.argv)
  File "/home/site/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/home/site/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/site/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/site/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/home/site/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 161, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "/home/site/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/db/migrations/executor.py", line 68, in migrate
    self.apply_migration(migration, fake=fake)
  File "/home/site/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/db/migrations/executor.py", line 102, in apply_migration
    migration.apply(project_state, schema_editor)
  File "/home/site/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/db/backends/schema.py", line 82, in __exit__
    self.execute(sql)
  File "/home/site/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/db/backends/schema.py", line 102, in execute
    cursor.execute(sql, params)
  File "/home/site/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/db/backends/utils.py", line 81, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/home/site/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/home/site/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/site/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/home/site/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 129, in execute
    return self.cursor.execute(query, args)
  File "/home/site/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
    self.errorhandler(self, exc, value)
  File "/home/site/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
django.db.utils.OperationalError: (1005, "Can't create table 'siteV2.#sql-2187_223e' (errno: 150)")






(ENV)[bijenkins@bmositedev siteAppsite]$ 
(ENV)[bijenkins@server site]$ python manage.py syncdb
DEBUG (0.001) SET SQL_AUTO_IS_NULL = 0; args=None
DEBUG (0.001) SHOW TABLES; args=None
DEBUG (0.001) SELECT `django_migrations`.`app`, `django_migrations`.`name` FROM `django_migrations`; args=()
Operations to perform:
  Synchronize unmigrated apps: allauth, app, tinymce, django_rq, suit
  Apply all migrations: account, explorer, thunderdomeV2, sessions, admin, documentation, sites, auth, thunderdome, contenttypes, dbmail, servicepathtable, socialaccount
Synchronizing apps without migrations:
DEBUG (0.001) SHOW TABLES; args=None
  Creating tables...
  Installing custom SQL...
  Installing indexes...
DEBUG (0.000) SET foreign_key_checks=0; args=None
DEBUG (0.000) SET foreign_key_checks=1; args=None
DEBUG (0.000) SET SQL_AUTO_IS_NULL = 0; args=None
DEBUG (0.000) SET foreign_key_checks=0; args=None
DEBUG (0.000) SET foreign_key_checks=1; args=None
DEBUG (0.000) SET SQL_AUTO_IS_NULL = 0; args=None
DEBUG (0.000) SET foreign_key_checks=0; args=None
DEBUG (0.000) SET foreign_key_checks=1; args=None
DEBUG (0.000) SET SQL_AUTO_IS_NULL = 0; args=None
DEBUG (0.000) SET foreign_key_checks=0; args=None
DEBUG (0.000) SET foreign_key_checks=1; args=None
DEBUG (0.000) SET SQL_AUTO_IS_NULL = 0; args=None
DEBUG (0.000) SET foreign_key_checks=0; args=None
DEBUG (0.000) SET foreign_key_checks=1; args=None
Running migrations:
  Applying dbmail.0006_auto_20150708_0714...DEBUG CREATE TABLE `dbmail_mailsubscription` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `backend` varchar(50) NOT NULL, `start_hour` varchar(5) NOT NULL, `end_hour` varchar(5) NOT NULL, `is_enabled` bool NOT NULL, `is_checked` bool NOT NULL, `defer_at_allowed_hours` bool NOT NULL, `address` varchar(60) NOT NULL, `user_id` integer NULL); (params [])
DEBUG (0.000) SET SQL_AUTO_IS_NULL = 0; args=None
DEBUG (0.001) CREATE TABLE `dbmail_mailsubscription` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `backend` varchar(50) NOT NULL, `start_hour` varchar(5) NOT NULL, `end_hour` varchar(5) NOT NULL, `is_enabled` bool NOT NULL, `is_checked` bool NOT NULL, `defer_at_allowed_hours` bool NOT NULL, `address` varchar(60) NOT NULL, `user_id` integer NULL); args=[]
Traceback (most recent call last):
  File "manage.py", line 17, in <module>
    execute_from_command_line(sys.argv)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/core/management/base.py", line 533, in handle
    return self.handle_noargs(**options)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 27, in handle_noargs
    call_command("migrate", **options)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/core/management/__init__.py", line 115, in call_command
    return klass.execute(*args, **defaults)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 161, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/db/migrations/executor.py", line 68, in migrate
    self.apply_migration(migration, fake=fake)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/db/migrations/executor.py", line 102, in apply_migration
    migration.apply(project_state, schema_editor)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/db/migrations/migration.py", line 108, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/db/migrations/operations/models.py", line 36, in database_forwards
    schema_editor.create_model(model)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/db/backends/schema.py", line 261, in create_model
    self.execute(sql, params)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/db/backends/schema.py", line 102, in execute
    cursor.execute(sql, params)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/db/backends/utils.py", line 81, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 129, in execute
    return self.cursor.execute(query, args)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
    self.errorhandler(self, exc, value)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
django.db.utils.OperationalError: (1050, "Table 'dbmail_mailsubscription' already exists")


mysql> use DBtest
Database changed
mysql> show tables;
+----------------------------------------+
| Tables_in_DBtest                     |
+----------------------------------------+
| Inventory_Warehouse                    |
| Inventory_Warehouse_copy               |
| Replace_List                           |
| ThunderDome                            |
| ThunderDome_End                        |
| ThunderDome_log                        |
| Waiting_Videos                         |
| account_emailaddress                   |
| account_emailconfirmation              |
| auditor_misc_lines                     |
| auditor_models                         |
| auth_group                             |
| auth_group_permissions                 |
| auth_permission                        |
| auth_user                              |
| auth_user_groups                       |
| auth_user_user_permissions             |
| dbmail_apikey                          |
| dbmail_mailbasetemplate                |
| dbmail_mailbcc                         |
| dbmail_mailcategory                    |
| dbmail_mailfile                        |
| dbmail_mailfromemail                   |
| dbmail_mailfromemailcredential         |
| dbmail_mailgroup                       |
| dbmail_mailgroupemail                  |
| dbmail_maillog                         |
| dbmail_maillogemail                    |
| dbmail_maillogexception                |
| dbmail_maillogtrack                    |
| dbmail_mailsubscription                |
| dbmail_mailtemplate                    |
| dbmail_mailtemplate_bcc_email          |
| dbmail_signal                          |
| dbmail_signaldeferreddispatch          |
| dbmail_signallog                       |
| django_admin_log     
@bijenkins
Copy link
Author

Failing Migration:

(ENV)[bijenkins@bmositedev siteAppsite]$ more /home/DOMAIN/bijenkins/.pyenv/versions/Graba/lib/python2.7/site-packages/dbmail/migrations/0006_auto_20150708_0714.py
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
from django.conf import settings


class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('dbmail', '0005_auto_20150506_2201'),
    ]

    operations = [
        migrations.CreateModel(
            name='MailSubscription',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('backend', models.CharField(max_length=50, choices=[(b'dbmail.backends.mail', 'MailBox'), (b'dbmail.backends.push', 'Push'), (b'dbmail.backends.sms', 'SMS'), (b'dbmail.backends.t
ts', 'TTS')])),
                ('start_hour', models.CharField(default=b'00:00', max_length=5)),
                ('end_hour', models.CharField(default=b'23:59', max_length=5)),
                ('is_enabled', models.BooleanField(default=True, db_index=True)),
                ('is_checked', models.BooleanField(default=False, db_index=True)),
                ('defer_at_allowed_hours', models.BooleanField(default=False)),
                ('address', models.CharField(max_length=60)),
                ('user', models.ForeignKey(verbose_name='User', blank=True, to=settings.AUTH_USER_MODEL, null=True)),
            ],
            options={
                'verbose_name': 'Mail Subscription',
            },
        ),
    ]

@gotlium
Copy link
Collaborator

gotlium commented Oct 23, 2015

I can not reproduce this problem. What I did:

git checkout d4b59d00144a84a90c5ad0fe2bbca9dce980de04
./manage.py syncdb
./manage.py migrate
git checkout master
./manage.py migrate

All is working fine without broken migrations.
Env: Django 1.4.20, MySQL 5.6.26, Python 2.7.10

Test migration from PyPi (Django 1.4):

cd /tmp
mkvirtualenv dbmail-test
pip install Django==1.4.20 django-db-mailer==2.0.2 MySQL-python==1.2.5 South==1.0.2
django-admin.py startproject testing; cd testing/
cat >> ./testing/settings.py  << END
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'dbmail_migration',
        'USER': 'root',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
    },
}
INSTALLED_APPS += (
    'south',
    'dbmail',
)
END
./manage.py syncdb --noinput
./manage.py migrate
pip install django-db-mailer==2.2.0
./manage.py migrate

and all is ok.

Test migration from PyPi (Django 1.8):

cd /tmp
mkvirtualenv dbmail-test
pip install Django==1.8.5 django-db-mailer==2.0.2 MySQL-python==1.2.5
django-admin.py startproject testing; cd testing/
cat >> ./testing/settings.py  << END
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'dbmail_migration',
        'USER': 'root',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
    },
}
INSTALLED_APPS += (
    'dbmail',
)
END
./manage.py migrate
pip install django-db-mailer==2.2.0
./manage.py migrate

... all is ok.

Give me a Django and MySQL version. And show me how I can reproduce this problem.

@bijenkins
Copy link
Author

Django 1.7.4 Mysql 5.5

@gotlium
Copy link
Collaborator

gotlium commented Oct 24, 2015

Can u run my examples?

@bijenkins
Copy link
Author

I can't, south doesn't work on 1.7. I've discovered that even if I remove all traces of this library, and drop tables it still happens.

I can get the error to change slightly. First it will output this:

(site)[bijenkins@bmositedev siteAppsite]$ python manage.py migrate
Operations to perform:
  Synchronize unmigrated apps: allauth, app, tinymce, django_rq, suit
  Apply all migrations: account, explorer, thunderdomeV2, sessions, admin, documentation, sites, auth, thunderdome, contenttypes, dbmail, servicepathtable, socialaccount
Synchronizing apps without migrations:
  Creating tables...
  Installing custom SQL...
  Installing indexes...
Running migrations:
  Applying dbmail.0006_auto_20150708_0714...Traceback (most recent call last):
  File "manage.py", line 17, in <module>
    execute_from_command_line(sys.argv)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 161, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/db/migrations/executor.py", line 68, in migrate
    self.apply_migration(migration, fake=fake)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/db/migrations/executor.py", line 102, in apply_migration
    migration.apply(project_state, schema_editor)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/db/backends/schema.py", line 82, in __exit__
    self.execute(sql)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/db/backends/schema.py", line 102, in execute
    cursor.execute(sql, params)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/db/backends/utils.py", line 81, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 129, in execute
    return self.cursor.execute(query, args)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
    self.errorhandler(self, exc, value)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
django.db.utils.OperationalError: (1005, "Can't create table 'siteV2.#sql-2187_2775' (errno: 150)")

And thend if I run Migrate again it goes to this output:

(site)[bijenkins@bmositedev siteAppsite]$ python manage.py migrate
Operations to perform:
  Synchronize unmigrated apps: allauth, app, tinymce, django_rq, suit
  Apply all migrations: account, explorer, thunderdomeV2, sessions, admin, documentation, sites, auth, thunderdome, contenttypes, dbmail, servicepathtable, socialaccount
Synchronizing apps without migrations:
  Creating tables...
  Installing custom SQL...
  Installing indexes...
Running migrations:
  Applying dbmail.0006_auto_20150708_0714...Traceback (most recent call last):
  File "manage.py", line 17, in <module>
    execute_from_command_line(sys.argv)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 161, in handle
    executor.migrate(targets, plan, fake=options.get("fake", False))
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/db/migrations/executor.py", line 68, in migrate
    self.apply_migration(migration, fake=fake)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/db/migrations/executor.py", line 102, in apply_migration
    migration.apply(project_state, schema_editor)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/db/migrations/migration.py", line 108, in apply
    operation.database_forwards(self.app_label, schema_editor, project_state, new_state)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/db/migrations/operations/models.py", line 36, in database_forwards
    schema_editor.create_model(model)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/db/backends/schema.py", line 261, in create_model
    self.execute(sql, params)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/db/backends/schema.py", line 102, in execute
    cursor.execute(sql, params)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/db/backends/utils.py", line 81, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/django/db/backends/mysql/base.py", line 129, in execute
    return self.cursor.execute(query, args)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
    self.errorhandler(self, exc, value)
  File "/home/DOMAIN/bijenkins/.pyenv/versions/site/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
django.db.utils.OperationalError: (1050, "Table 'dbmail_mailsubscription' already exists")

If I drop only that table, it reverts to the same first error. It seems that because this migration fails

python2.7/site-packages/dbmail/migrations/0006_auto_20150708_0714.py:

# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations
from django.conf import settings


class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('dbmail', '0005_auto_20150506_2201'),
    ]

    operations = [
        migrations.CreateModel(
            name='MailSubscription',
            fields=[
                ('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
                ('backend', models.CharField(max_length=50, choices=[(b'dbmail.backends.mail', 'MailBox'), (b'dbmail.backends.push', 'Push'), (b'dbmail.backends.sms', 'SMS'), (b'dbmail.backends.t
ts', 'TTS')])),
                ('start_hour', models.CharField(default=b'00:00', max_length=5)),
                ('end_hour', models.CharField(default=b'23:59', max_length=5)),
                ('is_enabled', models.BooleanField(default=True, db_index=True)),
                ('is_checked', models.BooleanField(default=False, db_index=True)),
                ('defer_at_allowed_hours', models.BooleanField(default=False)),
                ('address', models.CharField(max_length=60)),
                ('user', models.ForeignKey(verbose_name='User', blank=True, to=settings.AUTH_USER_MODEL, null=True)),
            ],
            options={
                'verbose_name': 'Mail Subscription',
            },
        ),
    ]

Also now when I uninstalled this software and dropped all tables to start fresh, it only creates the the mailsubscription table when running migrations.

image

When I research the first error I see that it looks like it's possibly related to the engine being used?

http://stackoverflow.com/questions/1457305/mysql-creating-tables-with-foreign-keys-giving-errno-150

@gotlium
Copy link
Collaborator

gotlium commented Oct 24, 2015

Use my examples above.

Test migration from PyPi (Django 1.7. Logs here https://gist.github.com/gotlium/87a2110bf1a7b43b7312):

cd /tmp
mkvirtualenv dbmail-test
pip install Django==1.7.10 django-db-mailer==2.0.2 MySQL-python==1.2.5
django-admin.py startproject testing; cd testing/
cat >> ./testing/settings.py  << END
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'dbmail_migration',
        'USER': 'root',
        'PASSWORD': '',
        'HOST': '',
        'PORT': '',
    },
}
INSTALLED_APPS += (
    'dbmail',
)
END
mysql -uroot -e 'create database dbmail_migration;'
./manage.py migrate
pip install django-db-mailer==2.2.0
./manage.py migrate

... all is ok.

@gotlium
Copy link
Collaborator

gotlium commented Oct 24, 2015

Check your DB engine. Maybe you are using InnoDB with MyISAM? or you have a problem with AUTH_USER_MODEL?

DEBUG (0.018) ALTER TABLE `dbmail_mailsubscription` ADD CONSTRAINT `dbmail_mailsubscription_user_id_440cd6feeb7e77b0_fk_auth_user_id` FOREIGN KEY (`user_id`) REFERENCES `auth_user` (`id`); args=[]

Also now when I uninstalled this software and dropped all tables to start fresh, it only creates the the mailsubscription table when running migrations.

you should cleanup migration table.

You are getting "Table 'dbmail_mailsubscription' already exists" because you are trying to re-run migrate command.

This problem can not be reproduced at standard installation without sources, settings and environment.
Sorry, but now I can not help to you, because all my tests is a successful.

@gotlium
Copy link
Collaborator

gotlium commented Oct 24, 2015

Additional info about alter fk (http://stackoverflow.com/questions/16866318/adding-foreign-key-fails-in-mysql-errno-150)

Its almost always a case of type mismatch between the primary key and the foreign key. Some tips to help in most cases:

  • Check to see if the table you area trying to relate uses an engine that supports foreing keys like InnoDB
  • Check if the columns are identically typed and nulled. eg (the 2 columns MUST match type, type size and sign)
  • Check if the destination column have an index or primary key.

Can you try mysql 5.6 to reproduce your problem?

@bijenkins
Copy link
Author

Sure enough.

msql>  ALTER TABLE `dbmail_mailsubscription` ADD CONSTRAINT `dbmail_mailsubscription_user_id_440cd6feeb7e77b0_fk_auth_user_id` FOREIGN KEY (`user_id`) REFERENCES `auth_user` (`id`);
ERROR 1005 (HY000): Can't create table 'GrabaV2.#sql-2187_27bf' (errno: 150)
mysql> 

So I'm looking and I see:

image

which isn't compatible with:

image

My question here is what is advised, changing the dbmail table is more advisable as it doesn't have any data in it doesn't seem to be a issue on any other ones. I removed migrations and removed tables again, ran from fresh install on same db, with same error. So obviously these don't match up.

I went in and changed the collation and engine of auth_user to as db_mailsubscription. This must have occurred by accident by myself. This database orignally lived on a 5.1 Mysql instance, and was upgraded to 5.5, new tables were being created in InnoDB, and old were created in MyISAM. Thank you for the help.

(site)[bijenkins@bmograbadev siteAppsite]$ python manage.py migrate
Operations to perform:
  Synchronize unmigrated apps: allauth, app, tinymce, django_rq, suit
  Apply all migrations: account, explorer, thunderdomeV2, sessions, admin, documentation, sites, auth, thunderdome, contenttypes, dbmail, servicepathtable, socialaccount
Synchronizing apps without migrations:
  Creating tables...
  Installing custom SQL...
  Installing indexes...
Running migrations:
  Applying dbmail.0001_initial... OK
  Applying dbmail.0002_auto_20150321_1539... OK
  Applying dbmail.0003_maillog_backend... OK
  Applying dbmail.0004_auto_20150321_2214... OK
  Applying dbmail.0005_auto_20150506_2201... OK
  Applying dbmail.0006_auto_20150708_0714... OK
  Applying dbmail.0007_auto_20150708_2016... OK
  Applying dbmail.0002_auto_20151013_1509... OK
  Applying dbmail.0008_merge... OK
  Applying dbmail.0009_auto_20151022_1953... OK
(site)[bijenkins@bmograbadev siteAppsite]$ 

@gotlium gotlium closed this as completed Oct 24, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants