[ 
https://issues.apache.org/jira/browse/METAMODEL-131?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14481601#comment-14481601
 ] 

ASF GitHub Bot commented on METAMODEL-131:
------------------------------------------

Github user kaspersorensen commented on a diff in the pull request:

    https://github.com/apache/metamodel/pull/14#discussion_r27818877
  
    --- Diff: 
jdbc/src/main/java/org/apache/metamodel/jdbc/JdbcCreateTableBuilder.java ---
    @@ -114,10 +114,23 @@ private String createSqlStatement(Table table) {
                        if (column.isNullable() != null && 
!column.isNullable().booleanValue()) {
                                sb.append(" NOT NULL");
                        }
    -                   if (column.isPrimaryKey()) {
    -                           sb.append(" PRIMARY KEY");
    -                   }
                }
    +        boolean primaryKeyExists = false;
    +        for(int i = 0 ; i < columns.length ; i++) {
    +           if(columns[i].isPrimaryKey()) {
    +                   if(!primaryKeyExists) {
    +                           sb.append(" , PRIMARY KEY(");
    +                           sb.append(columns[i].getName());
    +                           primaryKeyExists = true;
    +                   } else {
    +                           sb.append(",");
    +                           sb.append(columns[i].getName());
    +                   }
    +           }
    +        }
    +        if(primaryKeyExists) {
    +           sb.append(")");
    +        }
    --- End diff --
    
    Same comments as in AbstractTableCreationBuilder also applies here :)


> Create table with composite primary key gives error.
> ----------------------------------------------------
>
>                 Key: METAMODEL-131
>                 URL: https://issues.apache.org/jira/browse/METAMODEL-131
>             Project: Apache MetaModel
>          Issue Type: Bug
>            Reporter: Hosur Narahari
>
> Creating table with primary key is not possible since while generating sql we 
> add "PRIMARY KEY" keyword to each column which results in error in case of 
> composite primary keys. Below is the code.
> context.executeUpdate(new UpdateScript() {
>                       
>                       @Override
>                       public void run(UpdateCallback callback) {
>                               callback.createTable("amass", "test").
>                               
> withColumn("id").ofType(ColumnType.INTEGER).ofSize(16).asPrimaryKey()
>                               
> .withColumn("name").ofType(ColumnType.VARCHAR).ofSize(255).asPrimaryKey()
>                               .execute();
>                       }
>               });
> I am using mysql. Am I using it in the wrong way?



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to