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

Cannot add foreign key constraint' #1

Closed
jnhwkim opened this issue Feb 2, 2017 · 3 comments
Closed

Cannot add foreign key constraint' #1

jnhwkim opened this issue Feb 2, 2017 · 3 comments

Comments

@jnhwkim
Copy link
Contributor

jnhwkim commented Feb 2, 2017

createDatabaseTables() generates an error of

peewee.IntegrityError: (1215, 'Cannot add foreign key constraint')

since it involves making foreign keys to non-existent keys.

@abhshkdz
Copy link
Member

abhshkdz commented Feb 2, 2017

Hey @jnhwkim,

I can't seem to reproduce the error. Here's what I did:

  1. Cloned the repo.
  2. Copied mturk_scripts/example.config.json to mturk_scripts/config.json and filled in credentials.
  3. Created a new mysql user with privileges on visdial_chat database.
  4. from createDatabase import *, createDatabaseTables() works as expected, eventually printing All database tables created..

createDatabase.py does have the model definitions (including foreign key fields). Using mysql v14.14 and peewee v2.8.1.

@jnhwkim
Copy link
Contributor Author

jnhwkim commented Feb 3, 2017

Though I resolved the issue using schema.sql and setting default values for some fields appropriately, I leave this for the issue tracking.

As following the above instructions, I got:

>>> from createDatabase import *
>>> createDatabaseTables()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "createDatabase.py", line 92, in createDatabaseTables
    database.create_table(Answer)
  File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 3769, in create_table
    return self.execute_sql(*qc.create_table(model_class, safe))
  File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 3690, in execute_sql
    self.commit()
  File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 3514, in __exit__
    reraise(new_type, new_type(*exc_args), traceback)
  File "/usr/local/lib/python2.7/dist-packages/peewee.py", line 3683, in execute_sql
    cursor.execute(sql, params or ())
  File "/home/ubuntu/.local/lib/python2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
    self.errorhandler(self, exc, value)
  File "/home/ubuntu/.local/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
peewee.IntegrityError: (1215, 'Cannot add foreign key constraint')

I'm using mysql v14.14 and peewee v2.8.5.

When I created the database from schema.sql and some extra works for adjusting, it seems to work fine, I checked that the conversation on AMT was stored on the database. Creating tables in the script is a sequential task. How can it possible to make a foreign key to non-exist key, which is before-creation? I used to make tables without foreign keys first, after that, make foreign keys.

@abhshkdz
Copy link
Member

abhshkdz commented Feb 5, 2017

Thanks @jnhwkim!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants