Diesel only supports tables with primary keys

56 views Asked by At

I'm trying to create a migration with Diesel and an SQLite database and continue to get the following error:

Diesel only supports tables with primary keys. Table cycles has no primary key

However, I've whittled down the table to only have a primary key. Here's my entire up.sql file:

CREATE TABLE cycles (
    id INTEGER PRIMARY KEY,
);

For completeness, here's my migration command:

diesel migration run --database-url ~/.config/cycles/dev.sqlite

In case it helps, here's my diesel.toml:

# For documentation on how to configure this file,
# see https://diesel.rs/guides/configuring-diesel-cli

[print_schema]
file = "src-tauri/src/schema.rs"
custom_type_derives = ["diesel::query_builder::QueryId"]

[migrations_directory]
dir = "migrations"

What am I missing?

1

There are 1 answers

0
weiznich On

The provided migration does not reproduce the error in your question. For me applying the migration fails with the following error message:

$ cat migrations/2024-01-22-122221_test_stackoverflow/up.sql
-- Your SQL goes here

CREATE TABLE cycles (
    id INTEGER PRIMARY KEY,
);

$ diesel migration run --database-url /tmp/tests
Running migration 2024-01-22-122221_test_stackoverflow
Failed to run 2024-01-22-122221_test_stackoverflow with: near ")": syntax error

After fixing the syntax error pointed out in the error message diesel correctly generates the following schema.rs file for me:

// @generated automatically by Diesel CLI.

diesel::table! {
    cycles (id) {
        id -> Nullable<Integer>,
    }
}