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

Deal with null foreign keys #27

Merged
merged 1 commit into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,14 @@ def _add_related_rows_to_graph(row, row_node, graph):
except TypeError:
# This path for foreign keys.
related_row = related_rows
related_node = Node(
table=_get_table_name_from_row(related_row),
primary_key=_get_primary_key_from_row(related_row),
data=_get_data(related_row),
)
related.append((related_row, related_node))
# Ignore null foreign-keys.
if related_row is not None:
related_node = Node(
table=_get_table_name_from_row(related_row),
primary_key=_get_primary_key_from_row(related_row),
data=_get_data(related_row),
)
related.append((related_row, related_node))
unvisited = [
(row, node) for (row, node) in related
if node not in graph.nodes
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ requires = ["hatchling"]
build-backend = "hatchling.build"
[project]
name = "fk-graph"
version = "0.0.4"
version = "0.0.5"
authors = [
{ name="Andrew Curtis", email="[email protected]" },
{ name="John C Thomas" },
Expand Down
31 changes: 31 additions & 0 deletions test_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ def test_can_restrict_to_selected_tables(self):
any([n.table == "table_c" for n in graph.nodes])
)

def test_can_create_graph_when_some_rows_have_null_foreign_keys(self):
self._create_entries_with_null_foreign_key(self.engine)

graph = get_graph(self.engine, "table_a", "1")

# Check does not add null nodes to graph.
self.assertEqual(len(graph.nodes), 1)

def _create_single_table_no_relations(self, engine):
metadata_object = MetaData()
table_a = Table(
Expand Down Expand Up @@ -312,6 +320,29 @@ def _create_data_with_non_primary_key_non_foreign_kay_fields(self, engine):
)
conn.commit()

def _create_entries_with_null_foreign_key(self, engine):
metadata_object = MetaData()
table_a = Table(
"table_a",
metadata_object,
Column("id", Integer, primary_key=True),
Column("b_id", ForeignKey("table_b.id"), nullable=True),
)
table_b = Table(
"table_b",
metadata_object,
Column("id", Integer, primary_key=True),
)
metadata_object.create_all(engine)
with engine.connect() as conn:
conn.execute(
insert(table_a),
[
{"id": 1, "b_id": None},
]
)
conn.commit()


def _to_table_primary_keys(nodes):
return [
Expand Down