Adds foreign key constraint between spree_roles_users and spree_roles tables
# Source: https://github.com/solidusio/solidus | License: BSD-3-Clause | Copyright 2007-2014, Spree Commerce Inc. and other contributors
# Provided via justclaw.it.com; keep this notice with the code.
# frozen_string_literal: true
class AddForeignKeyToSpreeRoleUsers < ActiveRecord::Migration[7.0]
FOREIGN_KEY_VIOLATION_ERRORS = %w[PG::ForeignKeyViolation Mysql2::Error SQLite3::ConstraintException]
def up
# Uncomment the following code to remove orphaned records if this migration fails
#
# say_with_time "Removing orphaned roles/user join records (no corresponding role)" do
# Spree::RoleUser.left_joins(:role).where(spree_roles: { id: nil }).delete_all
# end
add_foreign_key :spree_roles_users, :spree_roles, column: :role_id, null: false
rescue ActiveRecord::S
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search for more like it.
Get a free API key