I am running jooq 3.10.1
Here is a code snippet with your SELECT statement and its output. As you
will see, the ALTER from the DDL print out do not have the names of the
constraints. But, as you have noted, with your select statement, clearly
the names of the constraints are in the DB system tables. We agree that
the generated java code has the constraint names. *However, the SQL
generated from the DSLContext ddl() method is not capturing the constraint
names.*
******* code *****
List<Schema> schemas = create.meta().getSchemas();
Queries ddl = null;
for (Schema s : schemas) {
if (s.getName().equals("APP")) {
ddl = create.ddl(s);
break;
}
}
for (Query query : ddl.queries()) {
System.out.println(query);
}
String sql = "SELECT
fc.constraintname,ft.tablename,fs.schemaname,fg.descriptor,pc.constraintname,ps.schemaname
FROM sys.sysconstraints fc JOIN sys.sysforeignkeys f ON f.constraintid =
fc.constraintid JOIN sys.sysconglomerates fg ON fg.conglomerateid =
f.conglomerateid JOIN sys.systables ft ON ft.tableid = fg.tableid JOIN
sys.sysschemas fs ON ft.schemaid = fs.schemaid JOIN sys.sysconstraints pc
ON pc.constraintid = f.keyconstraintid JOIN sys.sysschemas ps ON
pc.schemaid = ps.schemaid WHERE cast(fc.type as varchar(32672)) = 'F'";
System.out.println(create.resultQuery(sql).fetch().format());
****** end code ****
------------ Output from the code -----------
create schema "APP"
create table "APP"."ARC"(
"ID" integer not null,
"EDGE" integer,
"PRODUCT" integer,
"PRIORITY" integer,
constraint "SQL171130162547050"
primary key ("ID")
)
create table "APP"."DEMAND"(
"ID" integer not null,
"TERMINAL" integer,
"PRODUCT" integer,
"PERIOD" integer,
"AMOUNT" integer,
constraint "SQL171130162546990"
primary key ("ID")
)
create table "APP"."EDGE"(
"ID" integer not null,
"ORIGIN" integer,
"DESTINATION" integer,
"VEHICLE" integer,
"TRANSITTIME" double,
constraint "SQL171130162547031"
primary key ("ID")
)
create table "APP"."ERQ"(
"ID" integer not null,
"TERMINAL" integer,
"PRODUCT" integer,
"MODE" integer,
"AMOUNT" integer,
constraint "SQL171130162547010"
primary key ("ID")
)
create table "APP"."INGREDIENT"(
"ID" integer not null,
"RECIPE" integer,
"PRODUCT" integer,
"AMOUNT" integer,
constraint "SQL171130162546980"
primary key ("ID")
)
create table "APP"."MODE"(
"ID" integer not null,
"CODE" varchar(1),
"NAME" varchar(11),
constraint "SQL171130162546870"
primary key ("ID")
)
create table "APP"."OPERATION"(
"ID" integer not null,
"CODE" varchar(1),
"NAME" varchar(10),
constraint "SQL171130162546920"
primary key ("ID")
)
create table "APP"."PIPELINE"(
"ROUTE" integer not null,
"CAPACITY" integer,
"MAXBATCH" integer,
"MINBATCH" integer,
"MAXRATE" integer,
"MINRATE" integer,
"MININTERVAL" integer,
constraint "SQL171130162546960"
primary key ("ROUTE")
)
create table "APP"."PRODUCT"(
"ID" integer not null,
"NAME" varchar(4),
"ADDITIVE" boolean not null,
"DESCRIPTION" varchar(255),
constraint "SQL171130162546930"
primary key ("ID")
)
create table "APP"."RECIPE"(
"ID" integer not null,
"PRODUCT" integer,
"DESCRIPTION" varchar(255),
constraint "SQL171130162546971"
primary key ("ID")
)
create table "APP"."RESOURCE"(
"ID" integer not null,
"TERMINAL" integer,
"MODE" integer,
"NAME" varchar(30),
"THROUGHPUT" integer,
"SPACES" integer,
"VEHICLETIME" double,
"PUMPRATE" integer,
constraint "SQL171130162547000"
primary key ("ID")
)
create table "APP"."RESOURCEOPERATION"(
"ID" integer not null,
"TERMINALOPERATION1" integer,
"TERMINALOPERATION2" integer,
constraint "SQL171130162547030"
primary key ("ID")
)
create table "APP"."ROUTE"(
"ID" integer not null,
"NAME" varchar(30),
"CIRCULAR" boolean not null,
constraint "SQL171130162546950"
primary key ("ID")
)
create table "APP"."ROUTEARC"(
"ID" integer not null,
"ROUTE" integer,
"ARC" integer,
"STOP" integer,
"LOADING" varchar(255),
constraint "SQL171130162547060"
primary key ("ID")
)
create table "APP"."SCHEDULEDRECEIPT"(
"ID" integer not null,
"ARC" integer,
"PERIOD" integer,
"AMOUNT" integer,
constraint "SQL171130162547070"
primary key ("ID")
)
create table "APP"."SKU"(
"ID" integer not null,
"TERMINAL" integer,
"PRODUCT" integer,
"INVENTORY" integer,
"MAXAUTHORIZED" integer,
"MAXFILL" integer,
"CONTROLLIMIT" integer,
constraint "SQL171130162547001"
primary key ("ID")
)
create table "APP"."TERMINAL"(
"ID" integer not null,
"NAME" varchar(30),
"LOCATION" varchar(30),
"TYPE" varchar(10),
"OWNER" varchar(4),
"MAXHOURS" integer,
"INJECTION" boolean not null,
constraint "SQL171130162546940"
primary key ("ID")
)
create table "APP"."TERMINALOPERATION"(
"ID" integer not null,
"RESOURCE" integer,
"OPERATION" integer,
"PRODUCT" integer,
"SPACES" integer,
constraint "SQL171130162547020"
primary key ("ID")
)
create table "APP"."VEHICLETYPE"(
"ID" integer not null,
"NAME" varchar(20),
"MODE" integer,
"SIZE" integer,
"FLEET" integer,
"TANKS" integer,
constraint "SQL171130162546970"
primary key ("ID")
)
alter table "APP"."ARC"
add constraint
foreign key ("EDGE")
references "EDGE" ("ID")
alter table "APP"."ARC"
add constraint
foreign key ("PRODUCT")
references "PRODUCT" ("ID")
alter table "APP"."DEMAND"
add constraint
foreign key ("PRODUCT")
references "PRODUCT" ("ID")
alter table "APP"."DEMAND"
add constraint
foreign key ("TERMINAL")
references "TERMINAL" ("ID")
alter table "APP"."EDGE"
add constraint
foreign key ("DESTINATION")
references "RESOURCE" ("ID")
alter table "APP"."EDGE"
add constraint
foreign key ("ORIGIN")
references "RESOURCE" ("ID")
alter table "APP"."EDGE"
add constraint
foreign key ("VEHICLE")
references "VEHICLETYPE" ("ID")
alter table "APP"."ERQ"
add constraint
foreign key ("MODE")
references "MODE" ("ID")
alter table "APP"."ERQ"
add constraint
foreign key ("PRODUCT")
references "PRODUCT" ("ID")
alter table "APP"."ERQ"
add constraint
foreign key ("TERMINAL")
references "TERMINAL" ("ID")
alter table "APP"."INGREDIENT"
add constraint
foreign key ("PRODUCT")
references "PRODUCT" ("ID")
alter table "APP"."INGREDIENT"
add constraint
foreign key ("RECIPE")
references "RECIPE" ("ID")
alter table "APP"."PIPELINE"
add constraint
foreign key ("ROUTE")
references "ROUTE" ("ID")
alter table "APP"."RECIPE"
add constraint
foreign key ("PRODUCT")
references "PRODUCT" ("ID")
alter table "APP"."RESOURCE"
add constraint
foreign key ("MODE")
references "MODE" ("ID")
alter table "APP"."RESOURCE"
add constraint
foreign key ("TERMINAL")
references "TERMINAL" ("ID")
alter table "APP"."RESOURCEOPERATION"
add constraint
foreign key ("TERMINALOPERATION2")
references "TERMINALOPERATION" ("ID")
alter table "APP"."RESOURCEOPERATION"
add constraint
foreign key ("TERMINALOPERATION1")
references "TERMINALOPERATION" ("ID")
alter table "APP"."ROUTEARC"
add constraint
foreign key ("ARC")
references "ARC" ("ID")
alter table "APP"."ROUTEARC"
add constraint
foreign key ("ROUTE")
references "ROUTE" ("ID")
alter table "APP"."SCHEDULEDRECEIPT"
add constraint
foreign key ("ARC")
references "ARC" ("ID")
alter table "APP"."SKU"
add constraint
foreign key ("PRODUCT")
references "PRODUCT" ("ID")
alter table "APP"."SKU"
add constraint
foreign key ("TERMINAL")
references "TERMINAL" ("ID")
alter table "APP"."TERMINALOPERATION"
add constraint
foreign key ("OPERATION")
references "OPERATION" ("ID")
alter table "APP"."TERMINALOPERATION"
add constraint
foreign key ("PRODUCT")
references "PRODUCT" ("ID")
alter table "APP"."TERMINALOPERATION"
add constraint
foreign key ("RESOURCE")
references "RESOURCE" ("ID")
alter table "APP"."VEHICLETYPE"
add constraint
foreign key ("MODE")
references "MODE" ("ID")
+-----------------------------------+-----------------+----------+----------+------------------+----------+
|CONSTRAINTNAME |TABLENAME
|SCHEMANAME|DESCRIPTOR|CONSTRAINTNAME |SCHEMANAME|
+-----------------------------------+-----------------+----------+----------+------------------+----------+
|MODEVEHICLETYPE |VEHICLETYPE |APP |BTREE (3)
|SQL171130162546870|APP |
|MODERESOURCE |RESOURCE |APP |BTREE (3)
|SQL171130162546870|APP |
|MODEERQ |ERQ |APP |BTREE (4)
|SQL171130162546870|APP |
|OPERATIONTERMINALOPERATION |TERMINALOPERATION|APP |BTREE (3)
|SQL171130162546920|APP |
|PRODUCTRECIPE |RECIPE |APP |BTREE (2)
|SQL171130162546930|APP |
|PRODUCTINGREDIENT |INGREDIENT |APP |BTREE (3)
|SQL171130162546930|APP |
|PRODUCTDEMAND |DEMAND |APP |BTREE (3)
|SQL171130162546930|APP |
|PRODUCTSKU |SKU |APP |BTREE (3)
|SQL171130162546930|APP |
|PRODUCTERQ |ERQ |APP |BTREE (3)
|SQL171130162546930|APP |
|PRODUCTARC |ARC |APP |BTREE (3)
|SQL171130162546930|APP |
|PRODUCTTERMINALOPERATION |TERMINALOPERATION|APP |BTREE (4)
|SQL171130162546930|APP |
|TERMINALDEMAND |DEMAND |APP |BTREE (2)
|SQL171130162546940|APP |
|TERMINALSKU |SKU |APP |BTREE (2)
|SQL171130162546940|APP |
|TERMINALRESOURCE |RESOURCE |APP |BTREE (2)
|SQL171130162546940|APP |
|TERMINALERQ |ERQ |APP |BTREE (2)
|SQL171130162546940|APP |
|PIPELINEROUTE |PIPELINE |APP |BTREE (1)
|SQL171130162546950|APP |
|ROUTEROUTEARC |ROUTEARC |APP |BTREE (2)
|SQL171130162546950|APP |
|VEHICLETYPEEDGE |EDGE |APP |BTREE (4)
|SQL171130162546970|APP |
|RECIPEINGREDIENT |INGREDIENT |APP |BTREE (2)
|SQL171130162546971|APP |
|ORIGINEDGE |EDGE |APP |BTREE (2)
|SQL171130162547000|APP |
|DESTINATIONEDGE |EDGE |APP |BTREE (3)
|SQL171130162547000|APP |
|RESOURCETERMINALOPERATION |TERMINALOPERATION|APP |BTREE (2)
|SQL171130162547000|APP |
|TERMINALOPERATIONRESOURCEOPERATION |RESOURCEOPERATION|APP |BTREE (2)
|SQL171130162547020|APP |
|TERMINALOPERATIONRESOURCEOPERATION1|RESOURCEOPERATION|APP |BTREE (3)
|SQL171130162547020|APP |
|EDGEARC |ARC |APP |BTREE (2)
|SQL171130162547031|APP |
|ARCSHIPMENT |SCHEDULEDRECEIPT |APP |BTREE (2)
|SQL171130162547050|APP |
|ARCROUTEARC |ROUTEARC |APP |BTREE (3)
|SQL171130162547050|APP |
+-----------------------------------+-----------------+----------+----------+------------------+----------+
On Thu, Nov 30, 2017 at 10:31 AM, Lukas Eder <[email protected]> wrote:
> Hi Manuel,
>
> Thanks for your script. I've added the two additional tables to it:
>
> CREATE TABLE Edge (
> ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY,
> PRIMARY KEY (ID));
>
> CREATE TABLE Product (
> ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY,
> PRIMARY KEY (ID));
>
> CREATE TABLE Arc (
> ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY,
> Edge INTEGER DEFAULT 0,
> Product INTEGER DEFAULT 0,
> Priority INTEGER DEFAULT 1,
> PRIMARY KEY (ID));
>
> ALTER TABLE Arc ADD CONSTRAINT EdgeArc FOREIGN KEY (Edge) REFERENCES Edge
> (ID);
> ALTER TABLE Arc ADD CONSTRAINT ProductArc FOREIGN KEY (Product) REFERENCES
> Product (ID);
>
>
> And then ran the query that the jOOQ code generator runs:
>
> SELECT
> fc.constraintname,
> ft.tablename,
> fs.schemaname,
> fg.descriptor,
> pc.constraintname,
> ps.schemaname
> FROM
> sys.sysconstraints fc
> JOIN
> sys.sysforeignkeys f ON f.constraintid = fc.constraintid
> JOIN
> sys.sysconglomerates fg ON fg.conglomerateid = f.conglomerateid
> JOIN
> sys.systables ft ON ft.tableid = fg.tableid
> JOIN
> sys.sysschemas fs ON ft.schemaid = fs.schemaid
> JOIN
> sys.sysconstraints pc ON pc.constraintid = f.keyconstraintid
> JOIN
> sys.sysschemas ps ON pc.schemaid = ps.schemaid
> WHERE
> cast(fc.type as varchar(32672)) = 'F'
>
>
> But I don't see anything wrong with the output:
>
> CONSTRAINTNAME |TABLENAME |SCHEMANAME |DESCRIPTOR
> |CONSTRAINTNAME |SCHEMANAME |
> -----------------------|---------------------|-----------|--
> ------------|---------------------|-----------|
> EDGEARC |ARC |TEST |BTREE (2)
> |SQL171130171546430 |TEST |
> PRODUCTARC |ARC |TEST |BTREE (3)
> |SQL171130171546540 |TEST |
>
>
> The generated code on my side is correct as well:
>
> public static final ForeignKey<ArcRecord, EdgeRecord> EDGEARC =
> createForeignKey(org.jooq.test.derby.generatedclasses.Keys.SQL171130171546430,
> Arc.ARC, "EDGEARC", Arc.EDGE);
> public static final ForeignKey<ArcRecord, ProductRecord>
> PRODUCTARC =
> createForeignKey(org.jooq.test.derby.generatedclasses.Keys.SQL171130171546540,
> Arc.ARC, "PRODUCTARC", Arc.PRODUCT);
>
> When running this program:
>
> import org.jooq.SQLDialect;
> import org.jooq.impl.DSL;
> import org.jooq.test.derby.generatedclasses.Tables;
>
> public class Derby {
> public static void main(String[] args) throws Exception {
> System.out.println(
> DSL.using(SQLDialect.DERBY).ddl(Tables.ARC)
> );
> }
> }
>
>
> I'm getting the expected output:
>
> create table "TEST"."ARC"(
> "ID" int generated by default as identity not null,
> "EDGE" int default 0,
> "PRODUCT" int default 0,
> "PRIORITY" int default 1,
> constraint "SQL171130171546620"
> primary key ("ID"),
> constraint "EDGEARC"
> foreign key ("EDGE")
> references "TEST"."EDGE" ("ID"),
> constraint "PRODUCTARC"
> foreign key ("PRODUCT")
> references "TEST"."PRODUCT" ("ID")
> );
>
>
> So, clearly, there's a missing piece to help me reproduce this case...
> I've tried version 3.11-SNAPSHOT and 3.9.6. Any other hint, maybe, to help
> me reproduce this?
>
> Thanks,
> Lukas
>
> 2017-11-30 0:42 GMT+01:00 Manuel Rossetti <[email protected]>:
>
>> I generated the database using an sql script. Here is a snippet of that
>> script for the ARC table. The behavior is not specific to this table. It
>> occurs in all the generated DDL.
>>
>> CREATE TABLE Arc (
>> ID INTEGER NOT NULL GENERATED ALWAYS AS IDENTITY,
>> Edge INTEGER DEFAULT 0,
>> Product INTEGER DEFAULT 0,
>> Priority INTEGER DEFAULT 1,
>> PRIMARY KEY (ID));
>>
>> /* Foreign keys for Arc */
>> ALTER TABLE Arc ADD CONSTRAINT EdgeArc FOREIGN KEY (Edge) REFERENCES Edge
>> (ID);
>> ALTER TABLE Arc ADD CONSTRAINT ProductArc FOREIGN KEY (Product)
>> REFERENCES Product (ID);
>>
>> So, notice that in my original SQL the constraints are named (e.g
>> 'EdgeArc')
>>
>> The Keys.java class seems to have captured the names.
>>
>> // ------------------------------------------------------------
>> -------------
>> // FOREIGN KEY definitions
>> // ------------------------------------------------------------
>> -------------
>>
>> public static final ForeignKey<ArcRecord, EdgeRecord> EDGEARC =
>> ForeignKeys0.EDGEARC;
>> public static final ForeignKey<ArcRecord, ProductRecord> PRODUCTARC =
>> ForeignKeys0.PRODUCTARC;
>>
>> However, the DDL generated statements are like this:
>>
>> create table "APP"."ARC"(
>> "ID" integer not null,
>> "EDGE" integer,
>> "PRODUCT" integer,
>> "PRIORITY" integer,
>> constraint "SQL171126155154580"
>> primary key ("ID")
>> )
>>
>> alter table "APP"."ARC"
>> add constraint
>> foreign key ("EDGE")
>> references "EDGE" ("ID")
>> alter table "APP"."ARC"
>> add constraint
>> foreign key ("PRODUCT")
>> references "PRODUCT" ("ID")
>>
>> So that the generated DDL are missing the constraint names.
>>
>> BTW, my intent is to use the generated DDL to essentially replicate the
>> database and restore the original schema, which is one of the use cases for
>> the DDL functionality. In the jOOQ manual, the generated alter statements
>> do have the names for the constraints. So, I don't see what is going on
>> here.
>>
>>
>>
>>
>>
>> On Tuesday, November 28, 2017 at 5:53:34 AM UTC-6, Lukas Eder wrote:
>>>
>>> Hi Manuel,
>>>
>>> Thank you very much for your message.
>>>
>>> In the generated SQL, there's a double whitespace, which seems to
>>> indicate that the name of your constraint was simply empty (""). I wonder
>>> why that is. Are the names registered correctly in the generated Keys.java
>>> class? Can you share the constraint definition here?
>>>
>>> Thanks,
>>> Lukas
>>>
>>> 2017-11-26 23:30 GMT+01:00 Manuel Rossetti <[email protected]>:
>>>
>>>> I believe that there is a problem with how the DDL statements are being
>>>> constructed. I would like to know if anyone else sees this as a problem.
>>>>
>>>> DSLContext create = DSL.using(getConnection(), SQLDialect.DERBY);
>>>>
>>>> Queries queries = create.ddl(getUserSchema());
>>>>
>>>> The "ALTER" queries do not have their constraints named. In the
>>>> exception below, the name of the constraint is expected but the keyword
>>>> "foreign" was encountered. In the original creation script for the
>>>> database all constraints were named but the name 'EdgeArc' was not captured
>>>> by the create.ddl() statement.
>>>>
>>>> ALTER TABLE Arc ADD CONSTRAINT EdgeArc FOREIGN KEY (Edge) REFERENCES
>>>> Edge (ID);
>>>>
>>>> SQLException when executing alter table "APP"."ARC" add constraint
>>>> foreign key ("EDGE") references "EDGE" ("ID")
>>>> java.sql.SQLSyntaxErrorException: Syntax error: Encountered "foreign"
>>>> at line 1, column 41.
>>>> at
>>>> org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown
>>>> Source)
>>>> at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown
>>>> Source)
>>>> at
>>>> org.apache.derby.impl.jdbc.TransactionResourceImpl.wrapInSQLException(Unknown
>>>> Source)
>>>> at
>>>> org.apache.derby.impl.jdbc.TransactionResourceImpl.handleException(Unknown
>>>> Source)
>>>> at org.apache.derby.impl.jdbc.EmbedConnection.handleException(Unknown
>>>> Source)
>>>> at org.apache.derby.impl.jdbc.ConnectionChild.handleException(Unknown
>>>> Source)
>>>> at org.apache.derby.impl.jdbc.EmbedStatement.execute(Unknown
>>>> Source)
>>>> at org.apache.derby.impl.jdbc.EmbedStatement.execute(Unknown
>>>> Source)
>>>> at dbtools.dbutil.EmbeddedDerbyDatabase.executeCommand(Embedded
>>>> DerbyDatabase.java:680)
>>>> at dbtools.dbutil.EmbeddedDerbyDatabase.executeCommands(Embedde
>>>> dDerbyDatabase.java:710)
>>>> at dbtools.dbutil.EmbeddedDerbyDatabase.duplicate(EmbeddedDerby
>>>> Database.java:210)
>>>> at dbtools.dbutil.EmbeddedDerbyDatabase.testWrting(EmbeddedDerb
>>>> yDatabase.java:910)
>>>> at dbtools.dbutil.EmbeddedDerbyDatabase.main(EmbeddedDerbyDatab
>>>> ase.java:882)
>>>> Caused by: ERROR 42X01: Syntax error: Encountered "foreign" at line 1,
>>>> column 41.
>>>> at org.apache.derby.iapi.error.StandardException.newException(Unknown
>>>> Source)
>>>> at org.apache.derby.iapi.error.StandardException.newException(Unknown
>>>> Source)
>>>> at
>>>> org.apache.derby.impl.sql.compile.ParserImpl.parseStatementOrSearchCondition(Unknown
>>>> Source)
>>>> at org.apache.derby.impl.sql.compile.ParserImpl.parseStatement(Unknown
>>>> Source)
>>>> at org.apache.derby.impl.sql.GenericStatement.prepMinion(Unknown
>>>> Source)
>>>> at org.apache.derby.impl.sql.GenericStatement.prepare(Unknown
>>>> Source)
>>>> at org.apache.derby.impl.sql.conn.GenericLanguageConnectionCont
>>>> ext.prepareInternalStatement(Unknown Source)
>>>>
>>>>
>>>> --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "jOOQ User Group" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>> an email to [email protected].
>>>> For more options, visit https://groups.google.com/d/optout.
>>>>
>>>
>>> --
>> You received this message because you are subscribed to the Google Groups
>> "jOOQ User Group" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> For more options, visit https://groups.google.com/d/optout.
>>
>
> --
> You received this message because you are subscribed to a topic in the
> Google Groups "jOOQ User Group" group.
> To unsubscribe from this topic, visit https://groups.google.com/d/
> topic/jooq-user/QJBMlfxOne4/unsubscribe.
> To unsubscribe from this group and all its topics, send an email to
> [email protected].
> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google Groups "jOOQ
User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.