Skip to content

Commit 74cba7e

Browse files
Merge pull request #202 from MathisVerstrepen/hotfix
Fix: "graphs.node_count does not exist" on new project
2 parents 3b584e3 + 6e08131 commit 74cba7e

File tree

6 files changed

+38
-11
lines changed

6 files changed

+38
-11
lines changed

api/app/database/pg/graph_ops/graph_config_crud.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ async def update_graph_name(
4141
if not isinstance(db_graph, Graph):
4242
raise HTTPException(status_code=404, detail=f"Graph with id {graph_id} not found")
4343

44-
return db_graph
44+
await session.refresh(db_graph, attribute_names=["nodes", "edges"])
45+
46+
db_graph.node_count = len(db_graph.nodes)
47+
48+
return db_graph
4549

4650

4751
async def toggle_graph_pin(pg_engine: SQLAlchemyAsyncEngine, graph_id: str, pinned: bool) -> Graph:
@@ -72,7 +76,11 @@ async def toggle_graph_pin(pg_engine: SQLAlchemyAsyncEngine, graph_id: str, pinn
7276
if not isinstance(db_graph, Graph):
7377
raise HTTPException(status_code=404, detail=f"Graph with id {graph_id} not found")
7478

75-
return db_graph
79+
await session.refresh(db_graph, attribute_names=["nodes", "edges"])
80+
81+
db_graph.node_count = len(db_graph.nodes)
82+
83+
return db_graph
7684

7785

7886
class GraphConfigUpdate(BaseModel):
@@ -135,7 +143,11 @@ async def update_graph_config(
135143
if not isinstance(db_graph, Graph):
136144
raise HTTPException(status_code=404, detail=f"Graph with id {graph_id} not found")
137145

138-
return db_graph
146+
await session.refresh(db_graph, attribute_names=["nodes", "edges"])
147+
148+
db_graph.node_count = len(db_graph.nodes)
149+
150+
return db_graph
139151

140152

141153
async def get_canvas_config(pg_engine: SQLAlchemyAsyncEngine, graph_id: str) -> GraphConfigUpdate:

api/app/database/pg/graph_ops/graph_crud.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ async def get_all_graphs(engine: SQLAlchemyAsyncEngine, user_id: str) -> list[Gr
3838
.subquery()
3939
)
4040

41-
# Main query to select Graph and the node_count from the subquery
41+
# Main query to select the Graph object and the node_count as separate columns
4242
stmt = (
4343
select(Graph, node_count_subquery.c.node_count)
4444
.outerjoin(node_count_subquery, Graph.id == node_count_subquery.c.graph_id)
@@ -48,6 +48,7 @@ async def get_all_graphs(engine: SQLAlchemyAsyncEngine, user_id: str) -> list[Gr
4848

4949
result = await session.exec(stmt) # type: ignore
5050

51+
# Manually process the results and assign the count to the property
5152
graphs = []
5253
for graph, count in result.all():
5354
graph.node_count = count or 0
@@ -84,6 +85,8 @@ async def get_graph_by_id(engine: SQLAlchemyAsyncEngine, graph_id: str) -> Compl
8485
if not db_graph:
8586
raise HTTPException(status_code=404, detail=f"Graph with id {graph_id} not found")
8687

88+
db_graph.node_count = len(db_graph.nodes)
89+
8790
complete_graph_response = CompleteGraph(
8891
graph=db_graph,
8992
nodes=db_graph.nodes,
@@ -169,6 +172,11 @@ async def persist_temporary_graph(
169172
raise HTTPException(
170173
status_code=500, detail="Unexpected error: Retrieved object is not of type Graph."
171174
)
175+
176+
await session.refresh(db_graph, attribute_names=["nodes", "edges"])
177+
178+
db_graph.node_count = len(db_graph.nodes)
179+
172180
return db_graph
173181

174182

api/app/database/pg/graph_ops/graph_node_crud.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,9 @@ async def update_graph_with_nodes_and_edges(
107107
raise e
108108

109109
await session.refresh(db_graph, attribute_names=["nodes", "edges"])
110+
111+
db_graph.node_count = len(db_graph.nodes)
112+
110113
return db_graph
111114

112115

api/app/database/pg/models.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from typing import Any, Optional
77

88
from models.auth import UserPass
9+
from pydantic import PrivateAttr, computed_field
910
from sqlalchemy import Column, ForeignKeyConstraint, Index, PrimaryKeyConstraint, func, select
1011
from sqlalchemy.dialects.postgresql import DOUBLE_PRECISION, JSONB, TEXT, TIMESTAMP
1112
from sqlalchemy.dialects.postgresql import UUID as PG_UUID
@@ -87,9 +88,16 @@ class Graph(SQLModel, table=True):
8788
nodes: list["Node"] = Relationship(back_populates="graph")
8889
edges: list["Edge"] = Relationship(back_populates="graph")
8990

90-
node_count: Optional[int] = (
91-
None # This will be set dynamically in the query, not stored in the database
92-
)
91+
_node_count: Optional[int] = PrivateAttr(default=None)
92+
93+
@computed_field # type: ignore[misc]
94+
@property
95+
def node_count(self) -> Optional[int]:
96+
return self._node_count
97+
98+
@node_count.setter
99+
def node_count(self, value: Optional[int]) -> None:
100+
self._node_count = value
93101

94102
__table_args__ = (Index("idx_graphs_user_updated_at", "user_id", "updated_at"),)
95103

api/app/database/redis/redis_ops.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ async def annotation_exists(self, remote_hash: str) -> bool:
7676
Returns:
7777
bool: True if the annotation exists, False otherwise.
7878
"""
79-
print(remote_hash)
8079
if not remote_hash:
8180
return False
8281
key = f"annotation:{remote_hash}"

api/app/services/stream.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,6 @@ async def propagate_stream_to_websocket(
200200
)
201201

202202
if not is_title_generation:
203-
from rich import print
204-
205-
print(messages)
206203
openRouterReq = OpenRouterReqChat(
207204
api_key=open_router_api_key,
208205
model=request_data.model,

0 commit comments

Comments
 (0)