# Guide: reading the schema in dev

> Useful queries to explore tables, FKs, and indexes locally.

## List tables

```sql
SELECT table_name
FROM information_schema.tables
WHERE table_schema = 'public'
  AND table_type = 'BASE TABLE'
ORDER BY 1;
```

## Columns of a table

```sql
SELECT column_name, data_type, is_nullable, column_default
FROM information_schema.columns
WHERE table_schema = 'public' AND table_name = 'tasks'
ORDER BY ordinal_position;
```

## Foreign keys

```sql
SELECT
  tc.table_name,
  kcu.column_name,
  ccu.table_name AS foreign_table,
  ccu.column_name AS foreign_column
FROM information_schema.table_constraints tc
JOIN information_schema.key_column_usage kcu
  ON tc.constraint_name = kcu.constraint_name
JOIN information_schema.constraint_column_usage ccu
  ON ccu.constraint_name = tc.constraint_name
WHERE tc.constraint_type = 'FOREIGN KEY'
  AND tc.table_schema = 'public'
  AND tc.table_name = 'tasks';
```

## Indexes

```sql
SELECT indexname, indexdef
FROM pg_indexes
WHERE schemaname = 'public' AND tablename = 'tasks';
```

## Soft-deleted count

```sql
SELECT
  count(*) FILTER (WHERE deleted_at IS NULL) AS active,
  count(*) FILTER (WHERE deleted_at IS NOT NULL) AS soft_deleted
FROM tasks
WHERE profile_id = $1;
```

## Dual users check

```sql
SELECT u.id, u.email, u.auth_user_id, au.email AS auth_email
FROM users u
LEFT JOIN "user" au ON au.id = u.auth_user_id
WHERE u.deleted_at IS NULL
LIMIT 20;
```

## Tips

- Use the `postgres` database in the connection string.  
- Prefer queries filtered by `profile_id`.  
- Do not touch Better Auth tables from ad-hoc SQL in prod.
