CoolFace
Apppublic

dokasuka/PaperScope.ai

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
models.py110 linesDownload Raw Back to root
1# models.py2import uuid3from sqlalchemy import create_engine, Column, String, DateTime, Text, Integer, Boolean, ForeignKey, Table, text4from sqlalchemy.orm import sessionmaker, declarative_base, relationship5from datetime import datetime6from config import DATABASE_URL7 8engine = create_engine(9    DATABASE_URL,10    connect_args={"check_same_thread": False}11)12 13# WALモードの有効化14with engine.begin() as conn:15    conn.execute(text("PRAGMA journal_mode=WAL"))16 17Base = declarative_base()18 19# 論文とカテゴリの多対多リレーションを実現する中間テーブル20paper_category_association = Table(21    'paper_category_association',22    Base.metadata,23    Column('paper_id', String, ForeignKey('papers.id')),24    Column('category_id', Integer, ForeignKey('categories.id'))25)26 27class Paper(Base):28    __tablename__ = "papers"29    30    # 内部ID:UUIDを自動生成して主キーとする31    id = Column(String, primary_key=True, default=lambda: str(uuid.uuid4()))32    33    # タイトル(必須)34    title = Column(String, nullable=False)35    36    # 論文の概要37    abstract = Column(Text)38    39    # 著者情報(JSON形式)40    authors = Column(Text)41    42    # 論文公開日43    published_date = Column(DateTime)44    45    # 発表先(会議名、ジャーナル名など)46    venue = Column(String)47    48    # 出典情報(JSON形式)49    source = Column(Text)50    51    # 引用数52    citation_count = Column(Integer)53    54    # 影響力のある引用数55    influential_citation_count = Column(Integer)56    57    # 参考文献数58    reference_count = Column(Integer)59    60    # 関連論文IDリスト(JSON形式)61    related_ids = Column(Text)62    63    # PDFファイルリンク64    pdf_url = Column(Text)65    66    # オープンアクセスフラグ67    is_open_access = Column(Boolean)68    69    # 出版形態(ジャーナル、会議、プレプリントなど)70    publication_type = Column(String)71    72    # レコード作成日時73    created_at = Column(DateTime, default=datetime.utcnow)74    75    # レコード最終更新日時76    updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)77    78    # カテゴリとのリレーション(課題2)79    categories = relationship("Category", secondary=paper_category_association, back_populates="papers")80    81    # 外部識別子とのリレーション82    external_identifiers = relationship("ExternalIdentifier", back_populates="paper", cascade="all, delete-orphan")83 84class ExternalIdentifier(Base):85    __tablename__ = "external_identifiers"86    id = Column(Integer, primary_key=True, autoincrement=True)87    88    # 対象の論文ID89    paper_id = Column(String, ForeignKey('papers.id'), nullable=False)90    91    # 識別子の種類(例:"arxiv", "doi", "preprint", "published" など)92    identifier_type = Column(String, nullable=False)93    94    # 識別子の値95    value = Column(String, nullable=False)96 97    paper = relationship("Paper", back_populates="external_identifiers")98 99class Category(Base):100    __tablename__ = "categories"101    id = Column(Integer, primary_key=True, autoincrement=True)102    name = Column(String, unique=True, nullable=False)103 104    papers = relationship("Paper", secondary=paper_category_association, back_populates="categories")105 106# テーブル作成107Base.metadata.create_all(bind=engine)108 109SessionLocal = sessionmaker(bind=engine, autoflush=False, expire_on_commit=False)110