/** * Adds a foreign key to the given table. The referenced key may be automatically created if addUniqueConstraint is true. The column names should be in key order. * if reference table is is another schema, they will be resolved during validation. * @param name * @param columnNames * @param referencedColumnNames, may be null to indicate that the primary key should be used. * @param referenceTable - schema qualified reference table name, can be from another schema * @param table * @param addUniqueConstraint - if true, if the referenced table columns do not match with either PK, or FK then a UNIQUE index on reference table is created. * @return * @throws MetadataException */ public ForeignKey addForeignKey(String name, List<String> columnNames, List<String> referencedColumnNames, String referenceTable, Table table) { ForeignKey foreignKey = new ForeignKey(); foreignKey.setParent(table); foreignKey.setColumns(new ArrayList<Column>(columnNames.size())); assignColumns(columnNames, table, foreignKey); foreignKey.setReferenceTableName(referenceTable); foreignKey.setReferenceColumns(referencedColumnNames); foreignKey.setName(name); setUUID(foreignKey); table.getForeignKeys().add(foreignKey); return foreignKey; }
public static ForeignKey createForeignKey(String name, Table group, List<Column> elements, KeyRecord primaryKey) { ForeignKey key = new ForeignKey(); key.setName(name); for (Column column : elements) { key.addColumn(column); } key.setPrimaryKey(primaryKey); group.getForeignKeys().add(key); return key; }