Skip to content

Add missing foreign key constraint for columns badges.crate_id. #1840

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE badges DROP CONSTRAINT fk_badges_crate_id;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
DELETE FROM badges WHERE crate_id NOT IN (SELECT id FROM crates);
ALTER TABLE badges
ADD CONSTRAINT fk_badges_crate_id FOREIGN KEY (crate_id) REFERENCES crates(id) ON DELETE CASCADE;
2 changes: 1 addition & 1 deletion src/schema.patch
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ index df884e4..18e08cd 100644
/// Representation of the `reserved_crate_names` table.
///
@@ -881,23 +901,25 @@ table! {
}

joinable!(api_tokens -> users (user_id));
joinable!(badges -> crates (crate_id));
joinable!(crate_owner_invitations -> crates (crate_id));
joinable!(crate_owners -> crates (crate_id));
-joinable!(crate_owners -> users (created_by));
Expand Down
1 change: 1 addition & 0 deletions src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,7 @@ table! {
}

joinable!(api_tokens -> users (user_id));
joinable!(badges -> crates (crate_id));
joinable!(crate_owner_invitations -> crates (crate_id));
joinable!(crate_owners -> crates (crate_id));
joinable!(crate_owners -> teams (owner_id));
Expand Down
5 changes: 4 additions & 1 deletion src/tests/load_foreign_key_constraints.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ SELECT relname, conname, pg_get_constraintdef(pg_constraint.oid, true) AS defini
FROM pg_attribute
INNER JOIN pg_class ON pg_class.oid = attrelid
LEFT JOIN pg_constraint ON pg_class.oid = conrelid AND ARRAY[attnum] = conkey
INNER JOIN pg_namespace ON pg_namespace.oid = pg_class.relnamespace
WHERE attname = $1
AND contype = 'f'
AND relkind = 'r'
AND (contype IS NULL OR contype = 'f')
AND nspname = 'public';