title: sequelize eager loading returns null for associated table record. Why?
body: <p>I want to retrieve data from an associated table but the returned value is null.
  This is the association</p>
  <pre><code> static associate(models) { 
        appointment.hasOne(models.member, {
          foreignKey: &quot;id&quot;,
          sourceKey: &quot;member_id&quot;,
          constraints: false,
        });
        appointment.hasOne(models.system_data, {
          foreignKey: &quot;id&quot;,
          sourceKey: &quot;facility_id&quot;,
          constraints: false,
          as: &quot;system_data&quot;,
        });
      }
  </code></pre>
  <p>Members association is returned correctly but when I try to get system_data I get null even if it is present in database.
  Here I try to get:</p>
  <pre><code> const getRelatedTableRecords = () =&gt;
          include?.split(&quot;,&quot;).map((table) =&gt; {
            if (schemaName === &quot;appointment&quot; &amp;&amp; table === &quot;system_data&quot;) {
              
              return { model: db[table], as: &quot;system_data&quot;, required: false };
            }
            return { model: db[table] };
          });
  </code></pre>
  <p>I don't understand why I can't get system_data. What might be the reasons?? Do you have any suggestions?</p>
  <p>Member object</p>
  <pre><code>class Member extends Model {
       
      static associate(models) {
        // define association here
        Member.hasMany(models.card, {
          foreignKey: &quot;card_id&quot;,
          sourceKey: &quot;card_id&quot;,
        });
        Member.hasMany(models.club, {
          foreignKey: &quot;id&quot;,
          sourceKey: &quot;club&quot;,
          constraints: false,
        });
        Member.hasMany(models.schedule, {
          foreignKey: &quot;id&quot;,
          sourceKey: &quot;schedule&quot;,
          constraints: false,
        });
        Member.hasMany(models.trainer, {
          foreignKey: &quot;id&quot;,
          sourceKey: &quot;trainer&quot;,
          constraints: false,
        });
        Member.hasMany(models.file, {
          foreignKey: &quot;owner_id&quot;,
          sourceKey: &quot;id&quot;,
          constraints: false,
        });
        Member.hasOne(models.facilities, {
          foreignKey: &quot;id&quot;,
          sourceKey: &quot;facility_id&quot;,
          constraints: false,
        });
      }
    }
    Member.init(
      {
        first_name: {
          type: DataTypes.STRING,
          allowNull: false,
        },
        last_name: {
          type: DataTypes.STRING,
          allowNull: false,
        },
        tc_no: {
          type: DataTypes.BIGINT,
          allowNull: false,
          unique: true,
        },
        password: {
          type: DataTypes.STRING,
        },
        card_id: {
          type: DataTypes.UUID,
          defaultValue: DataTypes.UUIDV4,
          unique: true,
          allowNull: false,
        },
        registered: {
          type: DataTypes.BOOLEAN,
          defaultValue: false,
          allowNull: false,
        },
        schedule: {
          type: DataTypes.INTEGER,
          references: {
            model: &quot;schedules&quot;,
            key: &quot;id&quot;,
          },
          onDelete: &quot;CASCADE&quot;,
        },
        club: {
          type: DataTypes.INTEGER,
          references: {
            model: &quot;clubs&quot;,
            key: &quot;id&quot;,
          },
          onDelete: &quot;CASCADE&quot;,
          allowNull: true,
        },
        trainer: {
          allowNull: true,
          type: DataTypes.INTEGER,
          references: {
            model: &quot;trainers&quot;,
            key: &quot;id&quot;,
          },
          onDelete: &quot;CASCADE&quot;,
        },
        birthplace: {
          type: DataTypes.STRING,
          allowNull: true,
        },
        birthdate: { type: DataTypes.DATE, allowNull: true },
        father_name: { type: DataTypes.STRING, allowNull: true },
        mother_name: { type: DataTypes.STRING, allowNull: true },
        gender: { type: DataTypes.STRING, allowNull: true },
        profession: { type: DataTypes.STRING, allowNull: true },
        address: { type: DataTypes.STRING, allowNull: true },
        phone_number: { type: DataTypes.STRING, allowNull: true },
        hes_code: { type: DataTypes.STRING, allowNull: true },
        blood_type: { type: DataTypes.STRING, allowNull: true },
        nationality: { type: DataTypes.STRING, allowNull: true },
        profile_photo: {
          type: DataTypes.STRING,
          allowNull: true,
        },
        file: {
          type: DataTypes.STRING,
          allowNull: true,
        },
        session_ids: { type: DataTypes.ARRAY(DataTypes.STRING) },
        facility_id: { type: DataTypes.INTEGER },
        following: { type: DataTypes.ARRAY(DataTypes.INTEGER) },
        creator: {
          type: DataTypes.INTEGER,
          allowNull: false,
          defaultValue: 0,
        },
        updater: {
          type: DataTypes.INTEGER,
          allowNull: false,
          defaultValue: 0,
        },
      },
      {
        sequelize,
        modelName: &quot;member&quot;,
        hooks: {
          afterCreate: (member, options) =&gt; {
            sequelize.models.card.create({
              card_id: member.card_id,
              credits: 0,
              creator: member.creator,
              updater: member.updater,
            });
          },
          beforeUpdate: (member, options) =&gt; {
            const changed = member.changed();
            if (changed?.includes(&quot;card&quot;)) {
              sequelize.models.member.update(
                { trainer: null },
                { where: { id: member.dataValues.id } }
              );
            }
            if (changed?.includes(&quot;trainer&quot;)) {
              sequelize.models.member.update(
                { club: null },
                { where: { id: member.dataValues.id } }
              );
            }
          },
        },
      }
    );
  
  </code></pre>
  <p>system_data obj</p>
  <pre><code> class System_data extends Model {
      
      static associate(models) {
        // define association here
        System_data.hasOne(models.facilities, {
          foreignKey: &quot;id&quot;,
          sourceKey: &quot;facility_id&quot;,
          constraints: false,
        });
      }
    }
    System_data.init(
      {
        app_id: { type: DataTypes.STRING },
        province: DataTypes.STRING,
        district: DataTypes.STRING,
        address: DataTypes.TEXT,
        phone: DataTypes.STRING,
        reset_credits: { type: DataTypes.BOOLEAN, defaultValue: false },
        asset_path: {
          type: DataTypes.STRING,
        },
        card_price: {
          type: DataTypes.DECIMAL,
          allowNull: false,
          defaultValue: 0.0,
        },
        ticket_price: {
          type: DataTypes.DECIMAL,
          allowNull: false,
          defaultValue: 0.0,
        },
        facility_id: { type: DataTypes.INTEGER },
        working_hours: { type: DataTypes.STRING },
        capacity: DataTypes.BIGINT,
        season_based: { type: DataTypes.BOOLEAN, defaultValue: false },
        appointment_based: { type: DataTypes.BOOLEAN, defaultValue: false },
        seasons: {
          
          type: DataTypes.ARRAY(DataTypes.STRING),
        },
         
        season_capacity: {
          type: DataTypes.INTEGER,
        },
      },
      {
        sequelize,
        modelName: &quot;system_data&quot;,
      }
    );
  </code></pre>