How to pin a field photo to a drawing revision in SQLite
On Tuesday, a crew photographs a doorway on sheet A101. On Thursday, a designer issues a new A101 with the doorway moved. A Friday report that opens the latest A101 shows the photo beside a doorway the crew never saw.
The image and both PDFs are intact. The bug is in the join. Here is a small way to reproduce that bug and test a fix.
The sample uses fictional projects and runs with Python 3 and its built-in sqlite3 module. Run the three blocks in order in the same Python session, or paste them into one file.
Record what was on screen at capture time
A sheet number identifies a drawing within a project. To identify the version the crew saw, also record the revision. In this example the three values together identify a row, and each row points to a separately stored PDF.
import sqlite3
db = sqlite3.connect(":memory:")
db.execute("PRAGMA foreign_keys = ON")
db.executescript("""
CREATE TABLE drawing_revision (
project TEXT NOT NULL,
sheet TEXT NOT NULL,
revision INTEGER NOT NULL,
file_key TEXT NOT NULL,
PRIMARY KEY (project, sheet, revision)
);
CREATE TABLE photo (
photo_id TEXT PRIMARY KEY NOT NULL,
project TEXT NOT NULL,
sheet TEXT NOT NULL,
revision INTEGER NOT NULL,
FOREIGN KEY (project, sheet, revision) REFERENCES drawing_revision (project, sheet, revision)
);
""")
add_revision = "INSERT INTO drawing_revision VALUES (?, ?, ?, ?)"
add_photo = "INSERT INTO photo VALUES (?, ?, ?, ?)"
db.execute(add_revision, ("west", "A101", 1, "west/a101-r1.pdf"))
db.execute(add_photo, ("p17", "west", "A101", 1))
db.executemany(add_revision, [
("west", "A101", 2, "west/a101-r2.pdf"),
("east", "A101", 3, "east/a101-r3.pdf"),
])
Enable foreign keys on each SQLite connection before opening a transaction. The NOT NULL declarations matter here. SQLite permits a composite foreign key to contain a null component without checking for a matching parent row. SQLite foreign key documentation
Test the report after a new revision arrives
Both queries below use the same fixture. The first reproduces the reporting bug. The second uses the version saved with the photo.
latest = db.execute("""
SELECT file_key FROM drawing_revision WHERE project = ? AND sheet = ? ORDER BY revision DESC LIMIT 1
""", ("west", "A101")).fetchone()[0]
captured = db.execute("""
SELECT r.file_key FROM photo AS p JOIN drawing_revision AS r USING (project, sheet, revision) WHERE p.photo_id = ?
""", ("p17",)).fetchone()[0]
assert latest == "west/a101-r2.pdf"
assert captured == "west/a101-r1.pdf"
print(latest, captured, sep="\n")
A fixture with only one revision would let either query pass. Keeping revision 2 in the test makes an accidental switch to the latest drawing visible.
Try a revision that belongs to another project
The East project also uses sheet A101. Its revision 3 exists, so a lookup by sheet and revision alone would find a plausible PDF. The database should reject a West photo that claims to use it.
try:
db.execute(add_photo, ("p18", "west", "A101", 3))
except sqlite3.IntegrityError:
print("Rejected revision 3 for west/A101")
else:
raise AssertionError("An unknown West revision was accepted")
The database can check that the recorded revision exists. The capture screen still has to supply the revision it actually displayed. An offline client can store that identifier with the pending photo and retry the same pair once the revision is available. Resolving βcurrent revisionβ when the upload finally succeeds would reintroduce the Friday-report bug.
One more detail lives outside the database. Give each revision its own file or immutable object version. If both rows point to a single PDF that gets overwritten, even the correct join will retrieve Thursdayβs drawing.
Filio prepared this tutorial using AI-generated text and code. The records are fictional, and the example was run against an in-memory SQLite database.
Comments
No comments yet. Start the discussion.