Building a Sources & Citations System in SQLModel

Day 91 of 100 Days Coding Challenge: Python

Today was a reminder that computers are ruthless sticklers for details. I decided to add sources linked to the detailed card—because just like in academic papers, you can’t just “borrow” information floating around the internet without tipping your hat to whoever wrote it first. Seems fair, right?

But of course, the minute I touched the database, I triggered the classic relationship drama. My code started yelling at me: “source not defined.” Over and over. It was like being haunted by an ex who just wouldn’t leave the room. I spent five solid hours untangling imports, relationships, and annotations, trying everything from brute force to delicate tweaks. Eventually, the stars aligned, and the errors gave up. Victory never felt so earned.

Today’s Motivation / Challenge

Why spend a day on sources? Because information without sources is gossip, and gossip doesn’t belong in a timeline app. Adding citations isn’t just academic—it’s about trust. If you’re building a history tool, you want to know where the facts came from. Think of it like putting a bibliography on your app so it won’t get expelled for plagiarism.

Purpose of the Code (Object)


This code connects events and civilizations in the database to their sources, so each detail card can show where the information came from. Instead of a bare timeline with floating facts, now you get credibility built in. It’s like footnotes, but without the foot pain.

AI Prompt (for reference)

Day 16 — Sources & citations

  • sources table; link events/civs to sources; show bibliography per page.

Accept: civ detail lists sources with links.

Functions & Features

  • Add and store sources in a dedicated table.
  • Link sources to both events and civilizations.
  • Display a neat bibliography with clickable references on detail pages.

Requirements / Setup

  • Python 3.11+
  • Install SQLModel and SQLAlchemy 2.0+

pip install sqlmodel sqlalchemy

Minimal Code Sample

from typing import TYPE_CHECKING

from sqlmodel import SQLModel, Field, Relationship

class Source(SQLModel, table=True):

    id: int = Field(primary_key=True)

    title: str

    # Source linked to events

    events: list[“Event”] = Relationship(back_populates=”sources”)

# Ensures circular imports don’t trip us up

if TYPE_CHECKING:

    from .models import Event

This shows how a Source model can safely connect to events without circular import chaos.

The Civilization Timeline Builder

Notes / Lessons Learned

Those “source not defined” errors? Almost always circular-import headaches. The fix: postpone annotation evaluation, only import for type checking, and pass actual class objects instead of string guesses. SQLAlchemy 2.0 makes you play by the rules, and if you don’t, it punishes you with cryptic error messages.

After five hours of trial and error, I realized programming is like fixing plumbing: leaks pop up where you least expect them, and you just have to stay calm, systematic, and maybe a little stubborn. My biggest takeaway? Debugging isn’t a sprint—it’s therapy with a keyboard.

Optional Ideas for Expansion

  • Add clickable links so users can jump straight to the source online.
  • Allow filtering events by source—for example, “show me all events documented by Herodotus.”
  • Add a “source credibility” rating, so you can separate legends from solid history.

Leave a Reply

Your email address will not be published. Required fields are marked *