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

KeyError / status 500 for table with references #79

Closed
curiousleo opened this issue Oct 17, 2021 · 2 comments
Closed

KeyError / status 500 for table with references #79

curiousleo opened this issue Oct 17, 2021 · 2 comments
Labels
bug Something isn't working

Comments

@curiousleo
Copy link

curiousleo commented Oct 17, 2021

Error message

Traceback (most recent call last):
  [...]
  File "/usr/local/lib/python3.9/site-packages/graphql/type/definition.py", line 212, in define_field_map
    field_map = field_map()
  File "/usr/local/lib/python3.9/site-packages/graphene/types/typemap.py", line 275, in construct_fields_for_type
    map = self.reducer(map, field.type)
  File "/usr/local/lib/python3.9/site-packages/graphene/types/field.py", line 119, in type
    return get_type(self._type)
  File "/usr/local/lib/python3.9/site-packages/graphene/types/utils.py", line 45, in get_type
    return _type()
  File "/usr/local/lib/python3.9/site-packages/datasette_graphql/utils.py", line 624, in getter
    return table_classes[table]
KeyError: 'Katalogwerte'
INFO:     172.17.0.1:43848 - "GET /Marktstammdatenregister/EinheitenSolar HTTP/1.1" 500 Internal Server Error

Full stack trace: traceback.txt

SQL schema

This happens when I try to open the page of a table "EinheitenSolar". It relates to the table "Katalogwerte" (mentioned in the traceback) in that the "EinheitenSolar" table contains columns which reference "Katalogwerte":

create table 'EinheitenSolar' (
  -- [...]
  'Land' integer not null references Katalogwerte(Id) deferrable initially deferred,
  'Bundesland' integer references Katalogwerte(Id) deferrable initially deferred,
  -- [...]

Note that without the GraphQL plugin, Datasette recognises the references declarations and displays the label_column as expected. So the tables themselves seem to have a structure that Datasette can generally work with, it's just this plugin that is unhappy.

Full SQL for "EinheitenSolar": EinheitenSolar.sql.txt

Screenshot

image

Versions

$ pip list | grep -E 'datasette(-graphql)?\s'
datasette                 0.58.1
datasette-graphql         1.5
@simonw simonw added the bug Something isn't working label Nov 17, 2021
@simonw
Copy link
Owner

simonw commented Nov 17, 2021

Thanks for the SQL file. I could replicate this bug locally like so:

wget https://github.com/simonw/datasette-graphql/files/7360568/EinheitenSolar.sql.txt
sqlite3 einheit.db < EinheitenSolar.sql.txt
datasette einheit.db

@simonw
Copy link
Owner

simonw commented Nov 17, 2021

OK, I see what is happening here. There's only one table in that file, but it's CREATE TABLE refers to other tables which don't currently exist.

I'm a little surprised that Datasette doesn't throw errors there to be honest - good that it doesn't!

I think I can fix this in the introspect_tables() function:

def introspect_tables(conn, datasette, db_name):
db = sqlite_utils.Database(conn)
table_names = db.table_names()
view_names = db.view_names()
table_metadata = {}
table_namer = Namer("t")
for table in table_names + view_names:
datasette_table_metadata = datasette.table_metadata(
table=table, database=db_name
)
columns = db[table].columns_dict
foreign_keys = []
pks = []
supports_fts = bool(datasette_table_metadata.get("fts_table"))
fks_back = []
if hasattr(db[table], "foreign_keys"):
# Views don't have this
foreign_keys = db[table].foreign_keys
pks = db[table].pks
supports_fts = bool(db[table].detect_fts()) or supports_fts
# Gather all foreign keys pointing back here
collected = []
for t in db.tables:
collected.extend(t.foreign_keys)
fks_back = [f for f in collected if f.other_table == table]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants