Skip to content

Table.transform() can leave foreign keys in place when drop_foreign_keys is an iterator #988

Backlog

Description

@Mint0424

Table.transform() accepts an iterable for drop_foreign_keys, but a one-shot iterator is exhausted while checking the first existing foreign key. The method can complete successfully while leaving a requested constraint in place. Passing the same values as a list works.

Reproduced on main at 74c0b6cdce3e9234cdbcc0e3fcb339d950f04f19, Python 3.12.13, SQLite 3.53.3.

from sqlite_utils import Database

db = Database(memory=True, execute_plugins=False)
db["parents"].create({"id": int}, pk="id")
children = db["children"].create(
    {"id": int, "first_parent": int, "second_parent": int},
    pk="id",
    foreign_keys=[
        ("first_parent", "parents", "id"),
        ("second_parent", "parents", "id"),
    ],
)
keep, drop = [fk.column for fk in children.foreign_keys]
children.transform(drop_foreign_keys=iter([drop]))
print([fk.column for fk in children.foreign_keys])

Expected: [keep]. Actual: [keep, drop]. Replacing iter([drop]) with [drop] produces the expected result on the same database setup.

The drop specifications need to be reusable across comparisons with each existing foreign key. I have a small patch and a regression test for this.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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