File size: 1,245 Bytes
3e1dccc
 
 
 
ccbd93c
3e1dccc
 
ccbd93c
3e1dccc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ccbd93c
3e1dccc
 
ccbd93c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from __future__ import annotations

from datetime import datetime

from sqlalchemy import Column, Float, Integer, String, ForeignKey, DateTime
from sqlalchemy.orm import relationship

from .db import Base


class Node(Base):
    __tablename__ = "nodes"

    id = Column(Integer, primary_key=True)
    name = Column(String, unique=True, nullable=False)
    node_type = Column(String, nullable=False)

    outgoing_edges = relationship(
        "Edge",
        foreign_keys="Edge.src_id",
        back_populates="src",
    )

    incoming_edges = relationship(
        "Edge",
        foreign_keys="Edge.dst_id",
        back_populates="dst",
    )


class Edge(Base):
    __tablename__ = "edges"

    id = Column(Integer, primary_key=True)

    src_id = Column(Integer, ForeignKey("nodes.id"), nullable=False)
    relation = Column(String, nullable=False)
    dst_id = Column(Integer, ForeignKey("nodes.id"), nullable=False)

    created_at = Column(DateTime, default=datetime.utcnow)
    weight = Column(Float, nullable=True)

    src = relationship("Node", foreign_keys=[src_id], back_populates="outgoing_edges")
    dst = relationship("Node", foreign_keys=[dst_id], back_populates="incoming_edges")