I am working on an inventory tracking app in Rails 4 with the intent of keeping a log of the transactions made by each user. I have set up three models with the following relationships:
Item: has_many :transactions
User: has_many :transactions
Transaction: belongs_to :user, belongs_to :item
The User ID and Item ID are foreign keys in the Transaction table to associate a user and item with each transaction.
The issue arises when I try to delete an item, or user. I want the transactions to remain, so I have not enabled dependent destroy, but Postgres foreign key constraints do not allow the deletion, with the following error:
ActiveRecord::InvalidForeignKey (PG::ForeignKeyViolation: ERROR: update or delete on table "items" violates foreign key constraint "fk_rails_37b3ea4e18" on table "transactions"
Is there a better way to set up this relationship that will allow transactions to be associated to a user and item, but still exist after the user or item has been deleted?