Created
July 14, 2018 03:55
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
source 'https://rubygems.org' | |
gem 'sequel' | |
gem 'fixture_dependencies' | |
gem 'sqlite3' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
all_users: | |
id: 1 | |
name: All Users | |
users: | |
- john |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
john: | |
id: 1 | |
group: all_users | |
name: John Smith |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.