Skip to content

Commit

Permalink
Simplify drop-foreign-key, and drop_foreign_keys, closes #177
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Sep 24, 2020
1 parent d13c123 commit 5a63b9e
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 18 deletions.
2 changes: 1 addition & 1 deletion docs/cli.rst
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ Every option for this table (with the exception of ``--pk-none``) can be specifi
``--default-none column``
Remove the default value for this column.

``--drop-foreign-key col other_table other_column``
``--drop-foreign-key column``
Drop the specified foreign key.

If you want to see the SQL that will be executed to make the change without actually executing it, add the ``--sql`` flag. For example::
Expand Down
7 changes: 2 additions & 5 deletions docs/python-api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -995,17 +995,14 @@ The ``column_order=`` parameter can be used to change the order of the columns.
# Change column order
table.transform(column_order=("name", "age", "id")
You can use ``.transform()`` to remove foreign key constraints from a table. You will need to know the name of the column, the name of the table it points to and the name of the column it references on that other table.
You can use ``.transform()`` to remove foreign key constraints from a table.
This example drops two foreign keys - the one from ``places.country`` to ``country.id`` and the one from ``places.continent`` to ``continent.id``:
.. code-block:: python
db["places"].transform(
drop_foreign_keys=(
("country", "country", "id"),
("continent", "continent", "id"),
)
drop_foreign_keys=("country", "continent")
)
.. _python_api_transform_sql:
Expand Down
2 changes: 1 addition & 1 deletion sqlite_utils/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,7 @@ def rows(ctx, path, dbtable, nl, arrays, csv, no_headers, table, fmt, json_cols)
)
@click.option(
"--drop-foreign-key",
type=(str, str, str),
type=str,
multiple=True,
help="Drop this foreign key constraint",
)
Expand Down
4 changes: 1 addition & 3 deletions sqlite_utils/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -835,9 +835,7 @@ def transform_sql(
# foreign_keys
create_table_foreign_keys = []
for table, column, other_table, other_column in self.foreign_keys:
if (drop_foreign_keys is None) or (
(column, other_table, other_column) not in drop_foreign_keys
):
if (drop_foreign_keys is None) or (column not in drop_foreign_keys):
create_table_foreign_keys.append(
(rename.get(column) or column, other_table, other_column)
)
Expand Down
2 changes: 0 additions & 2 deletions tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -1542,8 +1542,6 @@ def test_transform_drop_foreign_key(db_path):
"places",
"--drop-foreign-key",
"country",
"country",
"id",
],
)
print(result.output)
Expand Down
7 changes: 1 addition & 6 deletions tests/test_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,12 +274,7 @@ def test_transform_drop_foreign_keys(fresh_db, use_pragma_foreign_keys):
),
]
# Drop two of those foreign keys
fresh_db["places"].transform(
drop_foreign_keys=(
("country", "country", "id"),
("continent", "continent", "id"),
)
)
fresh_db["places"].transform(drop_foreign_keys=("country", "continent"))
# Should be only one foreign key now
assert fresh_db["places"].foreign_keys == [
ForeignKey(table="places", column="city", other_table="city", other_column="id")
Expand Down

0 comments on commit 5a63b9e

Please sign in to comment.