class Sequel::Model::Associations::OneToManyAssociationReflection

Constants

FINALIZE_SETTINGS

Public Instance Methods

apply_eager_graph_limit_strategy(strategy, ds) click to toggle source

Support a lateral_subquery and correlated_subquery limit strategy when using eager_graph.

     # File lib/sequel/model/associations.rb
1094 def apply_eager_graph_limit_strategy(strategy, ds)
1095   case strategy
1096   when :correlated_subquery
1097     apply_correlated_subquery_limit_strategy(ds)
1098   else
1099     super
1100   end
1101 end
associated_object_keys() click to toggle source

The keys in the associated model’s table related to this association

     # File lib/sequel/model/associations.rb
1104 def associated_object_keys
1105   self[:keys]
1106 end
can_have_associated_objects?(obj) click to toggle source

one_to_many associations can only have associated objects if none of the :keys options have a nil value.

     # File lib/sequel/model/associations.rb
1110 def can_have_associated_objects?(obj)
1111   !self[:primary_keys].any?{|k| obj.get_column_value(k).nil?}
1112 end
cloneable?(ref) click to toggle source

one_to_many and one_to_one associations can be clones

     # File lib/sequel/model/associations.rb
1115 def cloneable?(ref)
1116   ref[:type] == :one_to_many || ref[:type] == :one_to_one
1117 end
default_key() click to toggle source

Default foreign key name symbol for key in associated table that points to current table’s primary key.

     # File lib/sequel/model/associations.rb
1121 def default_key
1122   :"#{underscore(demodulize(self[:model].name))}_id"
1123 end
finalize_settings() click to toggle source
     # File lib/sequel/model/associations.rb
1128 def finalize_settings
1129   FINALIZE_SETTINGS
1130 end
handle_silent_modification_failure?() click to toggle source

Handle silent failure of add/remove methods if raise_on_save_failure is false.

     # File lib/sequel/model/associations.rb
1133 def handle_silent_modification_failure?
1134   self[:raise_on_save_failure] == false
1135 end
predicate_key() click to toggle source

The hash key to use for the eager loading predicate (left side of IN (1, 2, 3))

     # File lib/sequel/model/associations.rb
1138 def predicate_key
1139   cached_fetch(:predicate_key){qualify_assoc(self[:key])}
1140 end
Also aliased as: qualified_key
primary_key() click to toggle source

The column in the current table that the key in the associated table references.

     # File lib/sequel/model/associations.rb
1144 def primary_key
1145   self[:primary_key]
1146 end
qualified_key()
Alias for: predicate_key
qualified_primary_key() click to toggle source

primary_key qualified by the current table

     # File lib/sequel/model/associations.rb
1149 def qualified_primary_key
1150   cached_fetch(:qualified_primary_key){qualify_cur(primary_key)}
1151 end
reciprocal_array?() click to toggle source

Whether the reciprocal of this association returns an array of objects instead of a single object, false for a one_to_many association.

     # File lib/sequel/model/associations.rb
1155 def reciprocal_array?
1156   false
1157 end
remove_before_destroy?() click to toggle source

Destroying one_to_many associated objects automatically deletes the foreign key.

     # File lib/sequel/model/associations.rb
1160 def remove_before_destroy?
1161   false
1162 end
remove_should_check_existing?() click to toggle source

The one_to_many association needs to check that an object to be removed already is associated.

     # File lib/sequel/model/associations.rb
1165 def remove_should_check_existing?
1166   true
1167 end
set_reciprocal_to_self?() click to toggle source

One to many associations set the reciprocal to self when loading associated records.

     # File lib/sequel/model/associations.rb
1170 def set_reciprocal_to_self?
1171   true
1172 end

Private Instance Methods

apply_correlated_subquery_limit_strategy(ds) click to toggle source

Use a correlated subquery to limit the dataset. Note that this will not work correctly if the associated dataset uses qualified identifers in the WHERE clause, as they would reference the containing query instead of the subquery.

     # File lib/sequel/model/associations.rb
1179 def apply_correlated_subquery_limit_strategy(ds)
1180   table = ds.first_source_table
1181   table_alias = ds.first_source_alias
1182   primary_key = associated_class.primary_key
1183   key = self[:key]
1184   cs_alias = :t1
1185   cs = associated_dataset.
1186     from(Sequel.as(table, :t1)).
1187     select(*qualify(cs_alias, primary_key)).
1188     where(Array(qualify(cs_alias, key)).zip(Array(qualify(table_alias, key)))).
1189     limit(*limit_and_offset)
1190   ds.where(qualify(table_alias, primary_key)=>cs)
1191 end
apply_filter_by_associations_limit_strategy(ds) click to toggle source

Support correlated subquery strategy when filtering by limited associations.

     # File lib/sequel/model/associations.rb
1242 def apply_filter_by_associations_limit_strategy(ds)
1243   case filter_by_associations_limit_strategy
1244   when :correlated_subquery
1245     apply_correlated_subquery_limit_strategy(ds)
1246   else
1247     super
1248   end
1249 end
apply_lateral_subquery_eager_graph_limit_strategy(ds) click to toggle source

Use a LATERAL subquery to limit the dataset. Note that this will not work correctly if the associated dataset uses qualified identifers in the WHERE clause, as they would reference the containing query instead of the subquery.

This does not contain the conditions that are necessary to join to the query, since the necessary qualifier is not passed as an argument.

     # File lib/sequel/model/associations.rb
1199 def apply_lateral_subquery_eager_graph_limit_strategy(ds)
1200   table_name = ds.first_source_alias
1201   qualifier = ds.opts[:eager_options][:implicit_qualifier]
1202   graph_conditions = self[:_graph_conditions]
1203 
1204   unless Sequel.condition_specifier?(graph_conditions)
1205     raise Error, "lateral_subquery eager graph limit strategy only supported when graph conditions are a hash or array of pairs"
1206   end
1207 
1208   ds.
1209     limit(*limit_and_offset).
1210     order(*self[:order]).
1211     where(graph_conditions.map{|k, v| [qualify(table_name, k), qualify(qualifier, v)]}).
1212     lateral
1213 end
apply_lateral_subquery_eager_limit_strategy(ds, ids, limit_and_offset) click to toggle source
     # File lib/sequel/model/associations.rb
1221 def apply_lateral_subquery_eager_limit_strategy(ds, ids, limit_and_offset)
1222   super.where(qualify(self[:model].table_name, self[:primary_key]) => ids)
1223 end
eager_loading_set_predicate_condition(ds, eo) click to toggle source

Avoid setting duplicate predicate condition when using the lateral subquery eager limit strategy.

     # File lib/sequel/model/associations.rb
1217 def eager_loading_set_predicate_condition(ds, eo)
1218   eager_limit_strategy == :lateral_subquery ? ds : super
1219 end
filter_by_associations_conditions_associated_keys() click to toggle source
     # File lib/sequel/model/associations.rb
1251 def filter_by_associations_conditions_associated_keys
1252   qualify(associated_class.table_name, self[:keys])
1253 end
filter_by_associations_conditions_key() click to toggle source
     # File lib/sequel/model/associations.rb
1255 def filter_by_associations_conditions_key
1256   qualify(self[:model].table_name, self[:primary_key_column])
1257 end
filter_by_associations_limit_alias_key() click to toggle source
     # File lib/sequel/model/associations.rb
1259 def filter_by_associations_limit_alias_key
1260   Array(filter_by_associations_limit_key)
1261 end
filter_by_associations_limit_aliases() click to toggle source
     # File lib/sequel/model/associations.rb
1263 def filter_by_associations_limit_aliases
1264   filter_by_associations_limit_alias_key.map(&:column)
1265 end
filter_by_associations_limit_key() click to toggle source
     # File lib/sequel/model/associations.rb
1267 def filter_by_associations_limit_key
1268   qualify(associated_class.table_name, associated_class.primary_key)
1269 end
lateral_subquery_filter_limit_strategy_conditions(obj) click to toggle source
     # File lib/sequel/model/associations.rb
1225 def lateral_subquery_filter_limit_strategy_conditions(obj)
1226   _lateral_subquery_filter_limit_strategy_conditions(obj, filter_by_associations_conditions_key, self[:key_method], self[:key])
1227 end
lateral_subquery_filter_limit_strategy_filter_dataset(ds, obj) click to toggle source
     # File lib/sequel/model/associations.rb
1233 def lateral_subquery_filter_limit_strategy_filter_dataset(ds, obj)
1234   ds.where(lateral_subquery_filter_limit_strategy_conditions(obj))
1235 end
lateral_subquery_filter_limit_strategy_filter_lateral_dataset(ds) click to toggle source
     # File lib/sequel/model/associations.rb
1229 def lateral_subquery_filter_limit_strategy_filter_lateral_dataset(ds)
1230   ds.where(Array(filter_by_associations_conditions_key).zip(Array(filter_by_associations_conditions_associated_keys)))
1231 end
lateral_subquery_filter_limit_strategy_lateral_dataset_select() click to toggle source
     # File lib/sequel/model/associations.rb
1237 def lateral_subquery_filter_limit_strategy_lateral_dataset_select
1238   qualified_primary_key
1239 end
predicate_key_methods() click to toggle source
     # File lib/sequel/model/associations.rb
1271 def predicate_key_methods
1272   self[:primary_keys]
1273 end
reciprocal_association?(assoc_reflect) click to toggle source
     # File lib/sequel/model/associations.rb
1275 def reciprocal_association?(assoc_reflect)
1276   super && self[:keys] == assoc_reflect[:keys] && primary_key == assoc_reflect.primary_key
1277 end
reciprocal_type() click to toggle source

The reciprocal type of a one_to_many association is a many_to_one association.

     # File lib/sequel/model/associations.rb
1280 def reciprocal_type
1281   :many_to_one
1282 end
true_eager_graph_limit_strategy() click to toggle source

Support automatic use of correlated subqueries if :ruby option is best available option, the database supports them, and either the associated class has a non-composite primary key or the database supports multiple columns in IN.

     # File lib/sequel/model/associations.rb
1287 def true_eager_graph_limit_strategy
1288   r = super
1289   ds = associated_dataset
1290   if r == :ruby && ds.supports_limits_in_correlated_subqueries? && (Array(associated_class.primary_key).length == 1 || ds.supports_multiple_column_in?) && (!offset || ds.supports_offsets_in_correlated_subqueries?)
1291     :correlated_subquery
1292   else
1293     r
1294   end
1295 end