Skip to content

Instantly share code, notes, and snippets.

@aheadley
Last active November 12, 2025 16:36
Show Gist options
  • Select an option

  • Save aheadley/4e521145457e31ce590dff5b34c5e20e to your computer and use it in GitHub Desktop.

Select an option

Save aheadley/4e521145457e31ce590dff5b34c5e20e to your computer and use it in GitHub Desktop.
deepseek-r1-demo
please finish the create table sql statement based on the other information in the file [webapp.py]
To properly create the `tasks` table based on the provided code, we need to define all necessary columns with their respective data types. Here's the complete SQL statement:```sql
CREATE TABLE tasks (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title VARCHAR(255) NOT NULL,
description TEXT,
due_at TIMESTAMP,
print_count INTEGER DEFAULT 0,
created_at TIMESTAMP WITH TIME ZONE,
updated_at TIMESTAMP WITH TIME ZONE,
completed_at TIMESTAMP WITH TIME ZONE
);
```This SQL statement creates a table that aligns with the data structures defined in your Python code, ensuring all necessary fields are included.
<details>
<summary>🧠 Thought</summary>
```TXT
Okay, so I'm trying to figure out how to complete the SQL CREATE TABLE statement for the tasks table based on the provided Python code. Let me go through this step by step.
First, I look at the code and see that there's a class called AbstractTaskData with attributes: title, description, due_at, and print_count. These are likely the columns in the tasks table. Then there's ConcreteTaskMetadata which includes created_at, updated_at, and completed_at. So these metadata fields should also be part of the table.
In the code, when setting up the database, the app.before_serving decorator runs a function that executes "CREATE TABLE tasks". But it doesn't specify the columns, so I need to add them.
Looking at the routes:
1. The get_task_by_id endpoint fetches all columns, which includes task_id, title, description, due_at, print_count, created_at, updated_at, and completed_at.
2. The update_task_by_id endpoint updates title, description, and due_at but doesn't touch the metadata fields, implying that these might be auto-updated elsewhere or handled by triggers.
3. The create_task endpoint inserts title, description, and due_at, but it's missing print_count, created_at, updated_at, and completed_at. So these probably have default values or are set upon creation.
Putting this together, the tasks table should have the following columns:
- id (since task_id is used as a foreign key in PrintRecord)
- title
- description
- due_at
- print_count
- created_at
- updated_at
- completed_at
I also need to decide on the data types for each column. Assuming standard SQL types:
- id would be an integer, primary key auto-incrementing.
- title is a string, maybe VARCHAR(255).
- description could be TEXT since it's optional and might hold longer text.
- due_at is a datetime, so TIMESTAMP.
- print_count is an integer, defaulting to 0.
- created_at, updated_at, and completed_at are all TIMESTAMP with time zone (timestamptz) as they likely represent times in different time zones.
Now, thinking about constraints:
- The id should be the primary key.
- Maybe a unique constraint on title if titles need to be unique, but the code doesn't enforce that yet.
- For print_count, having a default of 0 makes sense since new tasks start with zero prints unless specified.
Also, considering indexes. Since print_count is used in an ORDER BY clause, it might benefit from an index for faster sorting.
So, putting all this together, I'll structure the CREATE TABLE statement with these columns and appropriate data types.
```
</details>
#!/usr/bin/env python
from datetime import datetime
from dataclasses import dataclass
import logging
from quart import Quart, g
from quart_schema import QuartSchema, validate_request, validate_response
from quart_db import QuartDB
API_PREFIX = "/api/v0"
DB_URL = "sqlite:memory:"
app = Quart(__name__)
app_schema = QuartSchema(app)
app_db = QuartDB(app, url=DB_URL)
@dataclass
class AbstractTaskData:
title: str
description: str|None
due_at: datetime|None
print_count: int
@dataclass
class ConcreteTaskMetadata:
created_at: datetime|None
updated_at: datetime|None
completed_at: datetime|None
@dataclass
class ConcreteTask(AbstractTaskData, ConcreteTaskMetadata):
task_id: int
@dataclass
class PrintRecord:
job_id: int
task_id: int
created_at: datetime
def now() -> str:
return datetime.now().isoformat(timespec="seconds")
@app.before_serving
async def setup_devdb():
await g.connection.execute(
"CREATE TABLE tasks"
)
@app.post(API_PREFIX + "/task/print/<int:task_id>")
async def print_task_by_id(task_id: int):
pass
@app.get(API_PREFIX + "/task/<int:task_id>")
@validate_response(ConcreteTask)
async def get_task_by_id(task_id: int):
result = await g.connection.fetch_first(
"SELECT * FROM tasks WHERE id = :task_id",
{"task_id": task_id})
return ConcreteTask(**result)
@app.post(API_PREFIX + "/task/<int:task_id>")
@validate_request(AbstractTaskData)
@validate_response(ConcreteTask)
async def update_task_by_id(task_id: int, post_data: AbstractTaskData) -> ConcreteTask:
await g.connection.execute(
"UPDATE tasks SET title = :title, description = :description, due_at = :due_at WHERE id = :task_id",
{
"task_id": task_id,
"title": post_data.title,
"description": post_data.description,
"due_at": post_data.due_at,
})
return await get_task_by_id(task_id)
@app.get(API_PREFIX + "/task/")
@validate_response(list[ConcreteTask])
async def get_tasks(offset: int = 0, page_size: int = 10) -> list[ConcreteTask]:
result = await g.connection.fetch_all(
"SELECT * FROM tasks LIMIT :page_size ORDER BY print_count DESC",
{
"page_size": page_size,
},
)
return [ConcreteTask(**row) for row in result]
@app.post(API_PREFIX + "/task/")
@validate_request(AbstractTaskData)
@validate_response(ConcreteTask)
async def create_task(post_data: AbstractTaskData) -> ConcreteTask:
result = await g.connection.execute(
"INSERT INTO tasks (title, description, due_at) VALUES (:title, :description, :due_at)",
{
"title": post_data.title,
"description": post_data.description,
"due_at": post_data.due_at,
},
)
@app.route("/")
async def index():
return "hello world"
if __name__ == "__main__":
app.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment