Skip to content
Snippets Groups Projects
Commit f5949869 authored by Noah Jefferson Baumann's avatar Noah Jefferson Baumann
Browse files

fixed models.py to remove edges id

parent 000b0512
No related branches found
No related tags found
No related merge requests found
from flask import Flask, jsonify, request
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm import sessionmaker, scoped_session
from sqlalchemy import create_engine
from backend.models import Node, Edge
from dotenv import load_dotenv
......@@ -19,8 +19,10 @@ DATABASE_URL = os.getenv('DATABASE_URL')
if not DATABASE_URL:
raise ValueError("DATABASE_URL is not set in the environment variables")
# Create engine and session factory
engine = create_engine(DATABASE_URL)
Session = sessionmaker(bind=engine)
session_factory = sessionmaker(bind=engine)
Session = scoped_session(session_factory)
@app.route('/nodes', methods=['GET'])
def get_nodes():
......@@ -50,5 +52,10 @@ def get_edges():
except Exception as e:
return jsonify({'error': str(e)}), 500
# Ensure scoped_session is removed after each request
@app.teardown_appcontext
def remove_session(exception=None):
Session.remove()
if __name__ == '__main__':
app.run(debug=True)
from sqlalchemy import Column, Integer, Float, String
from sqlalchemy import Column, Integer, Float, String, PrimaryKeyConstraint
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
......@@ -29,10 +29,10 @@ class Node(Base):
'district': self.district
}
# Example for Edge model
# Example for Edge model with composite primary key
class Edge(Base):
__tablename__ = 'edges'
id = Column(String, primary_key=True)
source = Column(String)
target = Column(String)
label = Column(String)
......@@ -43,9 +43,12 @@ class Edge(Base):
distance = Column(Float)
edge_type = Column(String)
__table_args__ = (
PrimaryKeyConstraint('source', 'target'),
)
def to_dict(self):
return {
'id': self.id,
'source': self.source,
'target': self.target,
'label': self.label,
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment