Apache Metamodel How to add foreign keys while creating tables

198 views Asked by At

I've written a code to create some tables by apache metamodel:

dataContext.executeUpdate(new UpdateScript() {
        @Override
        public void run(UpdateCallback updateCallback) {
            updateCallback.createTable(schema, "aTable").withColumn("id").ofType(ColumnType.INTEGER)
            .withColumn("anotherTableId").ofType(ColumnType.INTEGER).execute();
            updateCallback.createTable(schema, "anotherTable").withColumn("id").ofType(ColumnType.INTEGER).execute();
        }
}

how can I add the relationship between these tables?

1

There are 1 answers

1
TomaszGuzialek On

You could try:

dataContext.executeUpdate(new UpdateScript() {
        @Override
        public void run(UpdateCallback updateCallback) {
            Table aTable = updateCallback.createTable(schema, "aTable")
                .withColumn("id").ofType(ColumnType.INTEGER)
                .withColumn("anotherTableId").ofType(ColumnType.INTEGER).execute();
            Table anotherTable = updateCallback.createTable(schema, "anotherTable")
                .withColumn("id").ofType(ColumnType.INTEGER).execute();

            MutableRelationship.createRelationship(
               anotherTable.getColumnByName("id"),
               aTable.getColumnByName("anotherTableId"));
        }
}