lib/declare_schema/model.rb
# Extend belongs_to so that it
# 1. creates a FieldSpec for the foreign key
# 2. declares an index on the foreign key
# 3. declares a foreign_key constraint
Extend belongs_to so that it
1. creates a FieldSpec for the foreign key
2. declares an index on the foreign key
3. declares a foreign_key constraint
Extend belongs_to so that it 1 . creates a FieldSpec for the foreign key 2 . declares an index on the foreign key 3 . declares a foreign_key constraint
def belongs_to(name, scope = nil, **options)
        column_options = {}
        column_options[:null] = if options.has_key?(:null)
                                  options.delete(:null)
                                elsif options.has_key?(:optional)
                                  options[:optional] 
                                end || false
        column_options[:default] = options.delete(:default) if options.has_key?(:default)
        if options.has_key?(:limit)
          options.delete(:limit)
          ActiveSupport::Deprecation.warn("belongs_to limit: is deprecated since it is now inferred")
        end
        index_options = {}
        index_options[:name]   = options.delete(:index) if options.has_key?(:index)
        index_options[:unique] = options.delete(:unique) if options.has_key?(:unique)
        index_options[:allow_equivalent] = options.delete(:allow_equivalent) if options.has_key?(:allow_equivalent)
        fk_options = options.dup
        fk_options[:constraint_name] = options.delete(:constraint) if options.has_key?(:constraint)
        fk_options[:index_name] = index_options[:name]
        fk = options[:foreign_key]&.to_s || "{name}_id"
        if !options.has_key?(:optional)
          options[:optional] = column_options[:null] 
        end
        fk_options[:dependent] = options.delete(:far_end_dependent) if options.has_key?(:far_end_dependent)
        super
        refl = reflections[name.to_s] or raise "Couldn't find reflection {name} in {reflections.keys}"
        fkey = refl.foreign_key or raise "Couldn't find foreign_key for {name} in {refl.inspect}"
        fkey_id_column_options = column_options.dup
         The one downside of this approach is that application code that asks the field_spec for the declared
         foreign key limit: will always get 8 back even if this is a grandfathered foreign key that points to
         a limit: 4 primary key. It seems unlikely that any application code would do this.
        fkey_id_column_options[:pre_migration] = ->(field_spec) do
          if (inferred_limit = _infer_fk_limit(fkey, refl))
            field_spec.sql_options[:limit] = inferred_limit
          end
        end
        declare_field(fkey.to_sym, :bigint, **fkey_id_column_options)
        if refl.options[:polymorphic]
          foreign_type = options[:foreign_type] || "{name}_type"
          _declare_polymorphic_type_field(foreign_type, column_options)
          index([foreign_type, fkey], index_options) if index_options[:name] != false
        else
          index(fkey, index_options) if index_options[:name] != false
          constraint(fkey, fk_options) if fk_options[:constraint_name] != false
        end
      end
def belongs_to ( name , scope = nil , ** options ) column_options = { } column_options [ :null ] = if options . has_key? ( :null ) options . delete ( :null ) elsif options . has_key? ( :optional ) options [ :optional ] end || false column_options [ :default ] = options . delete ( :default ) if options . has_key? ( :default ) if options . has_key? ( :limit ) options . delete ( :limit ) ActiveSupport :: Deprecation . warn ( "belongs_to limit: is deprecated since it is now inferred" ) end index_options = { } index_options [ :name ] = options . delete ( :index ) if options . has_key? ( :index ) index_options [ :unique ] = options . delete ( :unique ) if options . has_key? ( :unique ) index_options [ :allow_equivalent ] = options . delete ( :allow_equivalent ) if options . has_key? ( :allow_equivalent ) fk_options = options . dup fk_options [ :constraint_name ] = options . delete ( :constraint ) if options . has_key? ( :constraint ) fk_options [ :index_name ] = index_options [ :name ] fk = options [ :foreign_key ] &. to_s || "#{name}_id" if ! options . has_key? ( :optional ) options [ :optional ] = column_options [ :null ] end fk_options [ :dependent ] = options . delete ( :far_end_dependent ) if options . has_key? ( :far_end_dependent ) super refl = reflections [ name . to_s ] or raise "Couldn't find reflection #{name} in #{reflections.keys}" fkey = refl . foreign_key or raise "Couldn't find foreign_key for #{name} in #{refl.inspect}" fkey_id_column_options = column_options . dup fkey_id_column_options [ :pre_migration ] = -> ( field_spec ) do if ( inferred_limit = _infer_fk_limit ( fkey , refl ) ) field_spec . sql_options [ :limit ] = inferred_limit end end declare_field ( fkey . to_sym , :bigint , ** fkey_id_column_options ) if refl . options [ :polymorphic ] foreign_type = options [ :foreign_type ] || "#{name}_type" _declare_polymorphic_type_field ( foreign_type , column_options ) index ( [ foreign_type , fkey ] , index_options ) if index_options [ :name ] != false else index ( fkey , index_options ) if index_options [ :name ] != false constraint ( fkey , fk_options ) if fk_options [ :constraint_name ] != false end end
Extend belongs_to so that it
1. creates a FieldSpec for the foreign key
2. declares an index on the foreign key
3. declares a foreign_key constraint
Extend belongs_to so that it 1 . creates a FieldSpec for the foreign key 2 . declares an index on the foreign key 3 . declares a foreign_key constraint
# infer :null from :optional # infer :optional from :null # Note: the foreign key limit: should match the primary key limit:. (If there is a foreign key constraint, # those limits _must_ match.) We'd like to call _infer_fk_limit and get the limit right from the PK. # But we can't here, because that will mess up the autoloader to follow every belongs_to association right # when it is declared. So instead we assume :bigint (integer limit: 8) below, while also registering this # pre_migration: callback to double-check that assumption Just In Time--right before we generate a migration. # # The one downside of this approach is that application code that asks the field_spec for the declared # foreign key limit: will always get 8 back even if this is a grandfathered foreign key that points to # a limit: 4 primary key. It seems unlikely that any application code would do this.
name
<not_specific>
scope
<not_specific>
options
<not_specific>