database

Rules for defining database tables, data-access code, and schema-change migrations using SQLAlchemy and Alembic. Alembic is a tool that records and applies changes to a database structure over time.

In plain words
What is it for?
Use it when creating or changing database models, repositories, JSON data fields, soft deletion, constraints, or Alembic migrations. It also covers paging, sorting, commits, and refreshes.
Why use it?
Inconsistent models, defaults, queries, or migrations can cause data errors and make the application and database disagree. These rules establish one source of truth and checks for schema drift.

Cursor rule for Cursor

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add rules/lawofall/agentcore/database
Clone the repo
git clone --depth 1 https://github.com/Lawofall/AgentCore

Made for: Cursor.

Per session 0 Nothing until a file matches its globs; then the whole rule loads.
When invoked 609 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00000 $0.00609
Opus 5 $0.00000 $0.00304
Sonnet 5 $0.00000 $0.00122
Haiku 4.5 $0.00000 $0.00061

Measured yesterday against content hash 0257cacdcacf, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

database scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured yesterday.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

.cursor/rules/database.mdc · 50 lines

What it actually says

数据库 & ORM 规范

本规则是可执行约定(怎么写)。约定背后的设计理由(为何不用 ForeignKey、软删除架构选型、JSONB 索引策略、ORM 为何是 schema 真相源)见 docs/02-架构/核心接口定义.md §6.2,勿在此重复展开。

SQLAlchemy 模型

  • 使用 SQLAlchemy 2.0 Mapped + mapped_column 声明式
  • 所有表主键PG_UUID(as_uuid=False),应用层 str(uuid4()) 生成
  • 默认值必须用 server_default(seed 脚本和裸 SQL 不触发 Python 级 default)
  • 枚举字段用 CheckConstraint 固化合法值
  • 不使用 ForeignKey(应用层维护引用完整性),外键字段加 index=True
  • JSONB 字段同时设 default=dict/listserver_default=text("'{}'::jsonb")
  • JSONB 值与 UUID 列比较时用 cast(..., PG_UUID(as_uuid=False))
  • 软删除:设置 deleted_at 而非物理删除
  • ORM 是 schema 单一真相源alembic check 必须零漂移

标准字段

字段 类型 要求
id PG_UUID(as_uuid=False) 主键
created_at DateTime(timezone=True) server_default=func.now()
updated_at DateTime(timezone=True) server_default=func.now(), onupdate=func.now()
deleted_at DateTime(timezone=True), nullable 软删除标记
status String(20) 配 CheckConstraint

Repository 层

  • 只做数据访问,不含业务判断
  • 查询用 select() 构建式,不用 raw SQL
  • 分页返回 tuple[list[Model], int](数据 + 总数)
  • 默认按 created_at.desc() 排序
  • commit()refresh() 由 repository 负责

Alembic 迁移

  • schema 与数据严格分离:结构走迁移,初始数据走 seed 脚本
  • upgrade()downgrade() 必须成对实现
  • 复杂数据迁移用 op.execute()
Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. yesterday First seen · 50 lines · 0 tokens per session scan A 0257cacdcacf

Subscribe to this mod's changes

database is a cursor rule published in the GitHub repository Lawofall/AgentCore (78 stars, last pushed 2d ago), licensed MIT. It costs nothing until one of its globs matches a file; then it loads 609 tokens. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.