protected function addForeignKeys(DOMXPath $rootPath)
    {
        $fields = $this->getFields();
        $idMap = array_flip(array_map(function (MigrationField $field) {
            return $field->getId();
        }, $fields));

        /** @var MigrationField $field */
        foreach ($fields as $name => $field) {
            $usedId = $field->getId();
            $indexNodes = $this->getForeignKeysForField($field, $rootPath);

            if ($indexNodes && $indexNodes->length) {
                /** @var \DOMNode $indexNode */
                foreach ($indexNodes as $indexNode) {
                    $call = new ForeignKey();

                    $call
                        ->foreign($field->getName())
                        ->references('id')
                        ->on(
                            $rootPath->evaluate(
                                'string(.//link[@type="object" and @struct-name="db.mysql.Table" ' .
                                'and @key="referencedTable"])',
                                $indexNode
                            )
                        );

                    if ($rule = $rootPath->evaluate('string(./value[@key="deleteRule"])', $indexNode)) {
                        $call->onDelete(strtolower($rule));
                    } // if

                    if ($rule = $rootPath->evaluate('string(./value[@key="updateRule"])', $indexNode)) {
                        $call->onUpdate(strtolower($rule));
                    } // if

                    $call->isForMany((int)$rootPath->evaluate('number(./value[@key="many"])', $indexNode) === 1);
                } // foreach

                $this->addGenericCall($call);
            } // if
        } // foreach

        return $this;
    }