From bf99dac25cb441b06162dcd20bb175f86615d2a5 Mon Sep 17 00:00:00 2001 From: Ethan Girouard Date: Thu, 8 Feb 2024 18:38:18 -0500 Subject: [PATCH] Create albums table --- .../down.sql | 2 ++ .../up.sql | 13 ++++++++++++ src/schema.rs | 20 +++++++++++++++++++ 3 files changed, 35 insertions(+) create mode 100644 migrations/2024-02-06-150214_create_albums_table/down.sql create mode 100644 migrations/2024-02-06-150214_create_albums_table/up.sql diff --git a/migrations/2024-02-06-150214_create_albums_table/down.sql b/migrations/2024-02-06-150214_create_albums_table/down.sql new file mode 100644 index 0000000..31baf23 --- /dev/null +++ b/migrations/2024-02-06-150214_create_albums_table/down.sql @@ -0,0 +1,2 @@ +DROP TABLE album_artists; +DROP TABLE albums; diff --git a/migrations/2024-02-06-150214_create_albums_table/up.sql b/migrations/2024-02-06-150214_create_albums_table/up.sql new file mode 100644 index 0000000..154bab2 --- /dev/null +++ b/migrations/2024-02-06-150214_create_albums_table/up.sql @@ -0,0 +1,13 @@ +CREATE TABLE albums ( + id SERIAL PRIMARY KEY UNIQUE NOT NULL, + title VARCHAR NOT NULL, + release_date DATE +); + +-- A table to store artists for each album +-- Needed because an album can have multiple artists, but in Postgres we can't store an array of foreign keys +CREATE TABLE album_artists ( + album_id INTEGER REFERENCES albums(id) ON DELETE CASCADE NOT NULL, + artist_id INTEGER REFERENCES artists(id) ON DELETE CASCADE NULL, + PRIMARY KEY (album_id, artist_id) +); diff --git a/src/schema.rs b/src/schema.rs index 58b691a..1029c02 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -1,5 +1,20 @@ // @generated automatically by Diesel CLI. +diesel::table! { + album_artists (album_id, artist_id) { + album_id -> Int4, + artist_id -> Int4, + } +} + +diesel::table! { + albums (id) { + id -> Int4, + title -> Varchar, + release_date -> Nullable, + } +} + diesel::table! { artists (id) { id -> Int4, @@ -17,7 +32,12 @@ diesel::table! { } } +diesel::joinable!(album_artists -> albums (album_id)); +diesel::joinable!(album_artists -> artists (artist_id)); + diesel::allow_tables_to_appear_in_same_query!( + album_artists, + albums, artists, users, );