Skip to content

Foreign keys are not compatible with @cache #15

@sizhky

Description

@sizhky
schema = {'department': {'id': int, 'name': str},
 'students': {'columns': {'id': int, 'name': str, 'department_id': str},
  'foreign_keys': ['department_id']},
 'professors': {'columns': {'id': int, 'name': str, 'department_id': str},
  'foreign_keys': ['department_id']},
 'classes': {'columns': {'id': int, 'name': str, 'department_id': str},
  'foreign_keys': ['department_id']}}

db = database(DB)
for t in db.table_names(): db.t[t].drop()

for t,meta in schema.items():
    cs = meta.get('columns', meta)
    fks = meta.get('foreign_keys')
    db.create_table(t, cs, pk='id', foreign_keys=fks)

We get the error

TypeError                                 Traceback (most recent call last)
Cell In[3], line 16
     14 if isinstance(fks, L): fks = list(fks)
     15 print(fks)
---> 16 db.create_table(t, cs, pk='id', foreign_keys=fks)

File /opt/miniconda3/lib/python3.12/site-packages/sqlite_minutils/db.py:976, in Database.create_table(self, name, columns, pk, foreign_keys, column_order, not_null, defaults, hash_id, hash_id_columns, extracts, if_not_exists, replace, ignore, transform, strict)
    974 self.execute(sql)
    975 foreign_keys = self.resolve_foreign_keys(name, foreign_keys) if foreign_keys is not None else None
--> 976 created_table = self.table(
    977     name,
    978     pk=pk,
    979     foreign_keys=foreign_keys,
    980     column_order=column_order,
    981     not_null=not_null,
    982     defaults=defaults,
    983     hash_id=hash_id,
    984     hash_id_columns=hash_id_columns,
    985 )
    986 return cast(Table, created_table)

TypeError: unhashable type: 'list'

because self.table is @cache decorated and foreign keys are a list

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions