Problem with entity without PKs

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

Problem with entity without PKs

X Gylee
Whenever I define an entity without any primary key, OFBiz will throw an
exception about SQL syntax error.
The offending part is the "PRIMARY KEY ()" in the CREATE TABLE statement.
After browsing the Inet, someone pointed me out the file
framework/entity/src/org/ofbiz/entity/jdbc/DatabaseUtil.java.
After following the code to CREATE TABLE, it seems that the PRIMARY KEY is
always generated, and this will cause the error if no PK is found in the
entity definition.

Is this behavior necessary? I don't think all tables must have a primary key
if there is no use of it. Sometimes foreign keys are enough.
Below is the modification that I made to allow entities without PKs to be
generated. The patch is for Revision 574128. I am not sure though about the
side effects it creates.

$ diff -u DatabaseUtil.java DatabaseUtil.java.orig
--- DatabaseUtil.java   2007-10-01 14:35:25.000000000 +0700
+++ DatabaseUtil.java.orig      2007-10-01 14:45:07.000000000 +0700
@@ -1508,29 +1508,24 @@

             if (field.getIsPk()) {
                 if (this.datasourceInfo.alwaysUseConstraintKeyword) {
-                    sqlBuf.append(" CONSTRAINT NOT NULL");
+                    sqlBuf.append(" CONSTRAINT NOT NULL, ");
                 } else {
-                    sqlBuf.append(" NOT NULL");
+                    sqlBuf.append(" NOT NULL, ");
                 }
-            }
-
-            if (fieldIter.hasNext()) {
+            } else {
                 sqlBuf.append(", ");
             }
         }

-        if (entity.colNameString(entity.getPksCopy()) != "") {
-          String pkName = makePkConstraintName(entity,
this.datasourceInfo.constraintNameClipLength);
-          sqlBuf.append(", ");
-          if (this.datasourceInfo.usePkConstraintNames) {
-              sqlBuf.append("CONSTRAINT ");
-              sqlBuf.append(pkName);
-          }
-          sqlBuf.append(" PRIMARY KEY (");
-          sqlBuf.append(entity.colNameString(entity.getPksCopy()));
-          sqlBuf.append(")");
+        String pkName = makePkConstraintName(entity,
this.datasourceInfo.constraintNameClipLength);
+        if (this.datasourceInfo.usePkConstraintNames) {
+            sqlBuf.append("CONSTRAINT ");
+            sqlBuf.append(pkName);
         }
-
+        sqlBuf.append(" PRIMARY KEY (");
+        sqlBuf.append(entity.colNameString(entity.getPksCopy()));
+        sqlBuf.append(")");
+
         if (addFks) {
             // NOTE: This is kind of a bad idea anyway since ordering table
creations is crazy, if not impossible