question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

fails to display table with foreign keys

See original GitHub issue

Using the latest version from pip, I have the following issue:

When I click on a table that contains a foreign key, an Internal Server Error is caused with the following Traceback:

[2018-04-26 10:56:55,863] ERROR in app: Exception on /expression/ [GET]
Traceback (most recent call last):
  File "/storage/apps/easybuild/software/Anaconda3/5.0.1/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/storage/apps/easybuild/software/Anaconda3/5.0.1/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/storage/apps/easybuild/software/Anaconda3/5.0.1/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/storage/apps/easybuild/software/Anaconda3/5.0.1/lib/python3.6/site-packages/flask/_compat.py", line 33,in reraise
    raise value
  File "/storage/apps/easybuild/software/Anaconda3/5.0.1/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/storage/apps/easybuild/software/Anaconda3/5.0.1/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/storage/apps/easybuild/software/Anaconda3/5.0.1/lib/python3.6/site-packages/sqlite_web/sqlite_web.py", line 201, in inner
    return fn(table, *args, **kwargs)
  File "/storage/apps/easybuild/software/Anaconda3/5.0.1/lib/python3.6/site-packages/sqlite_web/sqlite_web.py", line 217, in table_structure
    ds_table = dataset[table]
  File "/storage/apps/easybuild/software/Anaconda3/5.0.1/lib/python3.6/site-packages/playhouse/dataset.py", line63, in __getitem__
    self.update_cache(table)
  File "/storage/apps/easybuild/software/Anaconda3/5.0.1/lib/python3.6/site-packages/playhouse/dataset.py", line92, in update_cache
    literal_column_names=True)
  File "/storage/apps/easybuild/software/Anaconda3/5.0.1/lib/python3.6/site-packages/playhouse/reflection.py", line 598, in generate_models
    literal_column_names=literal_column_names)
  File "/storage/apps/easybuild/software/Anaconda3/5.0.1/lib/python3.6/site-packages/playhouse/reflection.py", line 586, in introspect
    related_name=related_names.get(src))
  File "/storage/apps/easybuild/software/Anaconda3/5.0.1/lib/python3.6/site-packages/playhouse/reflection.py", line 120, in set_foreign_key
    self.rel_model = model_names[foreign_key.dest_table]
KeyError: 'sample'

The SQL of the table looks like

create table sample (sample varchar(80)
                     , patient varchar(80)
                     , cohort varchar(10)
                     , sample_type varchar(4)
                     , PRIMARY KEY (sample)
                     , FOREIGN KEY (cohort) references cohort(cohort)
                     , FOREIGN KEY (sample_type) references sample_type(sample_type))

The SQL of the referenced tables like

create table cohort (cohort varchar(10)
                     , name text
                     , PRIMARY KEY (cohort));

create table sample_type (numeric_code varchar(2)
                          , sample_type varchar(8)
                          , definition text
                          , PRIMARY KEY (sample_type)
                          , UNIQUE(numeric_code))

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
coleifercommented, May 4, 2018

The fix involved ensuring that dependent tables are specified when updating the cache with a newly-added table. This is fixed in peewee master branch. I will push a new release (will be 3.3.4) shortly.

0reactions
coleifercommented, May 4, 2018

Traceback:

Traceback (most recent call last):
  File "/home/charles/pypath/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/charles/pypath/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/charles/pypath/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/charles/pypath/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/charles/pypath/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "./sqlite_web.py", line 202, in inner
    return fn(table, *args, **kwargs)
  File "./sqlite_web.py", line 218, in table_structure
    ds_table = dataset[table]
  File "/home/charles/pypath/playhouse/dataset.py", line 63, in __getitem__
    self.update_cache(table)
  File "/home/charles/pypath/playhouse/dataset.py", line 92, in update_cache
    literal_column_names=True)
  File "/home/charles/pypath/playhouse/reflection.py", line 598, in generate_models
    literal_column_names=literal_column_names)
  File "/home/charles/pypath/playhouse/reflection.py", line 586, in introspect
    related_name=related_names.get(src))
  File "/home/charles/pypath/playhouse/reflection.py", line 120, in set_foreign_key
    self.rel_model = model_names[foreign_key.dest_table]
KeyError: u'gene'

After restarting the application, the table loads up fine, so this looks to be a cache-invalidation type of issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

3 common foreign key mistakes (and how to avoid them)
Dangling foreign keys 3. Not creating foreign key indexes Bonus: Not using foreign keys Key takeaway: think before you CREATE TABLE.
Read more >
Showing full MySQL foreign key errors - Stitcher.io
In case of a foreign key error when creating or altering a table, MySQL doesn't show the full message. You can read the...
Read more >
Bogus foreign key constraint fail - mysql - Stack Overflow
The innodb internal data dictionary is out of sync with the mysql one. You can see which table it was (one of them,...
Read more >
MySQL - Check or Not Foreign Key Constraints - SQLines
As an alternative, you can firstly create tables without foreign key constraints, load data and then create foreign keys using ALTER TABLE statements....
Read more >
13.1.20.5 FOREIGN KEY Constraints - MySQL :: Developer Zone
InnoDB does not currently support foreign keys for tables with user-defined partitioning. This includes both parent and child tables.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found