【问题标题】:Apache Metamodel How to add foreign keys while creating tablesApache Metamodel 如何在创建表时添加外键
【发布时间】:2016-11-13 09:05:38
【问题描述】:

我已经编写了一个代码来通过 apache 元模型创建一些表:

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();
        }
}

如何添加这些表之间的关系?

【问题讨论】:

    标签: java foreign-keys apache-metamodel


    【解决方案1】:

    你可以试试:

    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"));
            }
    }
    

    【讨论】:

    • 不幸的是 MutableRelationship.craeteRelationship() 只更新元模型而不是数据库后端。
    猜你喜欢
    • 2010-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多