protected function getForeignKeyReferenceCount($fkey, $max = null)
    {
        $count = 0;
        $tablename = $fkey->getReferencedTable()->getRawTableName();
        $columns = array();
        foreach ($this->getColumns() as $column) {
            foreach ($column->getForeignKeys() as $foreignKey) {
                // process only unique foreign key
                if (in_array($foreignKey->getId(), $columns)) {
                    continue;
                }
                $columns[] = $foreignKey->getId();
                if ($this->checkForeignKeyOwnerTableName($foreignKey, $tablename)) {
                    $count++;
                }
                if ($max && $count == $max) {
                    break;
                }
            }
        }

        return $count;
    }