Skip to content

Instantly share code, notes, and snippets.

@rintaun
Created July 14, 2018 03:55
Show Gist options
  • Save rintaun/9e8b1f293be2af94b3e2572f68cce17f to your computer and use it in GitHub Desktop.
Save rintaun/9e8b1f293be2af94b3e2572f68cce17f to your computer and use it in GitHub Desktop.
The cyclic dependency in this test generates a unique constraint violation due to how aliases are assigned to track which entries have already been inserted to the database.
source 'https://rubygems.org'
gem 'sequel'
gem 'fixture_dependencies'
gem 'sqlite3'
all_users:
id: 1
name: All Users
users:
- john
# frozen_string_literal: true
require 'sequel'
require 'fixture_dependencies'
$dir = File.dirname(File.expand_path(__FILE__))
FixtureDependencies.fixture_path = $dir
DB = Sequel.sqlite(File.join($dir, 'classmap_test.sqlite3'))
DB.create_table? :groups do
primary_key :id
text :name
end
DB.create_table? :users do
primary_key :id
foreign_key :group_id, :groups
text :name
end
module ClassMapTest
class User < Sequel::Model(:users)
many_to_one :group
end
FixtureDependencies.class_map[:user] = User
class Group < Sequel::Model(:groups)
one_to_many :users
end
FixtureDependencies.class_map[:group] = Group
end
begin
john = FixtureDependencies.load(:user__john)
p john
ensure
DB.drop_table :users
DB.drop_table :groups
end
john:
id: 1
group: all_users
name: John Smith
@rintaun
Copy link
Author

rintaun commented Jul 14, 2018

When load(:user__john) is executed, the resulting model is cached as :user__john. Then :class_map_test/group__all_users is created, which references :class_map_test/user__john -- since the name is different, the cached model is not found and it attempts to insert the record. The insert fails because it has already been inserted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment