0

I'm having this lines in every table in my db. I'm using django 1.8.

pub_date = models.DateTimeField(default=datetime.datetime.now)
author = models.ForeignKey(settings.AUTH_USER_MODEL)

I also tried the code below and I get the same error.

author = models.ForeignKey(User)

This is what I see in terminal using syncdb.

cy_thal is the name of my db.

C:\Python27\Lib\site-packages\django\core\management\commands\syncdb.py:24: Remo
vedInDjango19Warning: The syncdb command will be removed in Django 1.9
  warnings.warn("The syncdb command will be removed in Django 1.9", RemovedInDja
ngo19Warning)

Operations to perform:
  Synchronize unmigrated apps: chartit, pagination, staticfiles, autocomplete_li
ght, messages, bootstrap3_datetime, eReg, crispy_forms, bootstrap3
  Apply all migrations: admin, contenttypes, auth, sessions
Synchronizing apps without migrations:
  Creating tables...
    Creating table eReg_demographic
    Creating table eReg_icd_10
    Creating table eReg_pregnancy
    Creating table eReg_diagnosis
    Creating table eReg_clinical_data
    Creating table eReg_clinical_data_two
    Creating table eReg_a_b_sickle_thal
    Creating table eReg_redcell_enzyme_dis
    Creating table eReg_redcell_membrane_dis
    Creating table eReg_cong_dyseryth_anaemia
    Creating table eReg_ext_centers
    Running deferred SQL...
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "C:\Python27\Lib\site-packages\django\core\management\__init__.py", line
338, in execute_from_command_line
    utility.execute()
  File "C:\Python27\Lib\site-packages\django\core\management\__init__.py", line
330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python27\Lib\site-packages\django\core\management\base.py", line 390,
 in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Python27\Lib\site-packages\django\core\management\base.py", line 441,
 in execute
    output = self.handle(*args, **options)
  File "C:\Python27\Lib\site-packages\django\core\management\commands\syncdb.py"
, line 25, in handle
    call_command("migrate", **options)
  File "C:\Python27\Lib\site-packages\django\core\management\__init__.py", line
120, in call_command
    return command.execute(*args, **defaults)
  File "C:\Python27\Lib\site-packages\django\core\management\base.py", line 441,
 in execute
    output = self.handle(*args, **options)
  File "C:\Python27\Lib\site-packages\django\core\management\commands\migrate.py
", line 179, in handle
    created_models = self.sync_apps(connection, executor.loader.unmigrated_apps)

  File "C:\Python27\Lib\site-packages\django\core\management\commands\migrate.py
", line 317, in sync_apps
    cursor.execute(statement)
  File "C:\Python27\Lib\site-packages\django\db\backends\utils.py", line 79, in
execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "C:\Python27\Lib\site-packages\django\db\backends\utils.py", line 64, in
execute
    return self.cursor.execute(sql, params)
  File "C:\Python27\Lib\site-packages\django\db\utils.py", line 97, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "C:\Python27\Lib\site-packages\django\db\backends\utils.py", line 62, in
execute
    return self.cursor.execute(sql)
  File "C:\Python27\Lib\site-packages\django\db\backends\mysql\base.py", line 12
4, in execute
    return self.cursor.execute(query, args)
  File "C:\Python27\Lib\site-packages\MySQLdb\cursors.py", line 174, in execute
    self.errorhandler(self, exc, value)
  File "C:\Python27\Lib\site-packages\MySQLdb\connections.py", line 36, in defau
lterrorhandler
    raise errorclass, errorvalue
django.db.utils.OperationalError: (1005, "Can't create table 'cy_thal.#sql-f8c_9
' (errno: 150)")

Do you have any idea how to fix this?

Thank you in advance!

zinon
  • 4,427
  • 14
  • 70
  • 112
  • You should use the code given in the error to search more details about it: http://stackoverflow.com/questions/4063141/mysql-foreign-key-error-1005-errno-150 and http://stackoverflow.com/questions/9018584/error-code-1005-cant-create-table-errno-150. MySQL needs indexes to set the FK constraint, one of the two may not have been automatically created. – pawamoy May 04 '16 at 09:12
  • @Pawamoy And how can I do this from models.py in django? – zinon May 04 '16 at 09:15
  • 1
    Did you try to run `./manage.py migrate auth` before `./manage.py migrate` ? In some cases `auth` app must be migrated before the others. – pawamoy May 04 '16 at 11:07
  • @Pawamoy This is the solution! Please add it as an answer to accept it! – zinon May 04 '16 at 12:16

2 Answers2

0

Not completely sure which is the error, but I'd try the next.

After making changes to models execute these lines:
python manage.py makemigrations appname
python manage.py migrate appname

If error remains, try: python manage.py syncdb

EDIT: I edited my answer as Daniel Roseman suggested. Django 1.8 supports Migrations. No need to use South.

oz19
  • 1,616
  • 1
  • 17
  • 22
0

In some cases the auth app must be migrated before the other apps, because it generates all the permissions, and also the indexes of very often used models like User, which are required for setting foreign key constraints in other models.

Run ./manage.py migrate auth, then you should be able to do a standard ./manage.py migrate.

pawamoy
  • 3,382
  • 1
  • 26
  • 44