{% set statement = Migration.tableStatement(table, true) %}
{% set hasProcessedConstraint = false %}
{% for constraintName, constraint in constraints %}
    {%~ set constraintColumns = constraint['columns']|default([])|sort %}
    {%~ if constraint['type'] == 'foreign' %}
        {%~ set hasProcessedConstraint = true %}
        {%~ set columnsList = '\'' ~ constraint['columns'][0] ~ '\'' %}
        {%~ set storedColumnList = columnsList %}
        {%~ if constraint['columns']|length > 1 %}
            {%~ set storedColumnList = '[' ~ Migration.stringifyList(constraint['columns'], {'indent': 5}) ~ ']' %}
            {%~ set columnsList = '[' ~ Migration.stringifyList(constraint['columns'], {'indent': 6}) ~ ']' %}
        {%~ endif %}
        {%~ set record = Migration.storeReturnedData(table, storedColumnList) %}
        {%~ if constraint['references'][1] is iterable %}
            {%~ set columnsReference = '[' ~ Migration.stringifyList(constraint['references'][1], {'indent': 6}) ~ ']' %}
        {%~ else %}
            {%~ set columnsReference = '\'' ~ constraint['references'][1] ~ '\'' %}
        {%~ endif %}
        {%~ if statement is not defined %}
            {%~ set statement = Migration.tableStatement(table) %}
        {%~ endif %}
        {%~ if statement is not empty %}

        {{ statement | raw }}
            {%~ set statement = null %}
        {%~ endif %}
            ->addForeignKey(
                $this->foreignKey({{ columnsList | raw }})
                    ->setReferencedTable('{{ constraint['references'][0] }}')
                    ->setReferencedColumns({{ columnsReference | raw }})
                    ->setDelete('{{ Migration.formatConstraintAction(constraint['delete']) | raw }}')
                    ->setUpdate('{{ Migration.formatConstraintAction(constraint['update']) | raw }}')
                    ->setName('{{ constraintName }}')
            )
    {%~ endif %}
{% endfor %}
{% if Migration.wasTableStatementGeneratedFor(table) and hasProcessedConstraint %}
            ->update();
{% endif -%}
