block/buzz · error

insert community

Error message

insert community

What it means

Panic in the `make_community` fixture: `sqlx::query("INSERT INTO communities (id, host) VALUES ($1, $2)").execute(pool).await.expect("insert community")`. The expect fires when the INSERT fails — most commonly a unique-constraint violation on communities.host or a foreign-key/schema problem. The test generates a random host per call, so a duplicate means a collided or pre-existing row.

Source

Thrown at crates/buzz-db/src/store/community.rs:681

    use sqlx::PgPool;

    async fn setup_db() -> Db {
        let database_url = crate::test_support::database_url();
        let pool = PgPool::connect(&database_url)
            .await
            .expect("connect to test DB");
        Db::from_pool(pool)
    }

    async fn make_community(pool: &PgPool) -> Uuid {
        let id = Uuid::new_v4();
        let host = format!("communities-of-channels-{}.example", id.simple());
        sqlx::query("INSERT INTO communities (id, host) VALUES ($1, $2)")
            .bind(id)
            .bind(host)
            .execute(pool)
            .await
            .expect("insert community");
        id
    }

    async fn insert_channel(pool: &PgPool, community_id: Uuid, channel_id: Uuid) {
        let creator: Vec<u8> = vec![0u8; 32];
        sqlx::query(
            r#"
            INSERT INTO channels
                (id, community_id, name, channel_type, visibility, created_by)
            VALUES
                ($1, $2, $3, 'stream'::channel_type, 'open'::channel_visibility, $4)
            "#,
        )
        .bind(channel_id)
        .bind(community_id)
        .bind(format!("ch-{}", channel_id.simple()))
        .bind(&creator)
        .execute(pool)

View on GitHub (pinned to dad5a33865)

Solutions

  1. Reset the scratch/test database (drop and re-run migrations) to remove leftover rows blocking the unique host index.
  2. Include the random UUID in the host (it already does via id.simple()) — verify the collision is with an old row, not this test.
  3. Run migrations against TEST_DATABASE_URL before the suite.
  4. Log the underlying sqlx error (unwrap_or_else panic with {e}) to distinguish constraint violation from connection failure.

Example fix

// before
sqlx::query("INSERT INTO communities (id, host) VALUES ($1, $2)")
    .bind(id).bind(host).execute(pool).await.expect("insert community");
// after
sqlx::query("INSERT INTO communities (id, host) VALUES ($1, $2)")
    .bind(id).bind(host).execute(pool).await
    .unwrap_or_else(|e| panic!("insert community {id} host {host}: {e}"));
Defensive patterns

Strategy: try-catch

Validate before calling

// ensure a clean slate for the fixture
sqlx::query("DELETE FROM communities WHERE host = $1")
    .bind(&host)
    .execute(pool)
    .await
    .expect("clean stale community rows");

Try / catch

sqlx::query("INSERT INTO communities (id, host) VALUES ($1, $2)")
    .bind(id).bind(host).execute(pool).await
    .unwrap_or_else(|e| panic!("insert community {id} host {host}: {e}"));

Prevention

When it happens

Trigger: INSERT returns Err when the host value already exists (unique index), the communities table/columns don't exist (migrations not applied), the pool connection breaks mid-test, or a generated UUID/host collides with a leftover row from a previous crashed run.

Common situations: Stale rows in a shared test database from an earlier run that didn't clean up, migrations skipped, or tests run against a persistent (non-scratch) DB where the deterministic host format was reused.

Related errors


AI-assisted analysis of block/buzz@dad5a33865 (2026-08-30). Data as JSON: /api/errors/aa9f2550bcd83107. Report an issue: GitHub.