Skip to content

Table indexes are silently dropped from tables when foreign keys are added #633

@matdmiller

Description

@matdmiller

After creating a table with an index, if you add a foreign key constraint to the table it drops existing non-related indexes.

I have attached a minimal repro example screenshot and code.

Screenshot 2024-07-20 at 8 05 26 PM

Repro code

from sqlite_utils import Database

from pathlib import Path
db_fp = './db_repro_index.db'
Path(db_fp).unlink(missing_ok=True), Path(f'{db_fp}-shm').unlink(missing_ok=True),Path(f'{db_fp}-wal').unlink(missing_ok=True);

# db = database(db_fp)
db = Database(db_fp)

db['foo'].create(dict(id=int, name=str), pk='id', not_null=['name'])

db['foo'].create_index(['name'], unique=True)
db['foo'].indexes

db['bar'].create(dict(id=int, name=str, foo_id=int), pk='id', not_null=['name'])

db['bar'].create_index(['name'], unique=True)
db['bar'].indexes

db.add_foreign_keys((('bar','foo_id','foo','id'),))

db['bar'].indexes

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions