public function validatePivotTables($metadata)
    {
        $pivotTables = [];
        foreach ($metadata as $entityMetadata) {
            foreach ($entityMetadata['relations'] as $relationMetadata) {
                if (! empty($relationMetadata['pivotTable'])) {
                    $pivotTables[$entityMetadata['class'].$relationMetadata['relatedEntity']] = $relationMetadata;

                    if (isset($pivotTables[$relationMetadata['relatedEntity'].$entityMetadata['class']])) {
                        $relation1 = $pivotTables[$relationMetadata['relatedEntity'].$entityMetadata['class']];
                        $relation2 = $relationMetadata;

                        $error = null;

                        // check name
                        if ($relation1['pivotTable']['name'] != $relation2['pivotTable']['name']) {
                            $error = 'Different table names (compared '.$relation1['pivotTable']['name'].' with '.$relation2['pivotTable']['name'].').';
                        }

                        // check name
                        if (! empty(array_diff_key($relation1['pivotTable']['columns'], $relation2['pivotTable']['columns']))) {
                            $columns1 = implode(', ', array_keys($relation1['pivotTable']['columns']));
                            $columns2 = implode(', ', array_keys($relation2['pivotTable']['columns']));
                            $error = 'Different column names (compared '.$columns1.' with '.$columns2.').';
                        }

                        if ($error) {
                            throw new DomainException('Error syncing pivot tables for relations "'.$relation1['name'].'" in "'.$relation2['relatedEntity'].'" and "'.$relation2['name'].'" in "'.$relation1['relatedEntity'].'": '.$error);
                        }
                    }
                }
            }
        }
    }