Skip to content

Commit db01785

Browse files
committed
Fixed bug creating foreign keys for model fields with the same type as the parent model
1 parent 6835080 commit db01785

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

Diff for: src/javaxt/orm/Field.java

+11-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class Field {
2626
//**************************************************************************
2727
/** Creates a new instance of this class.
2828
*/
29-
protected Field(String name, String type){
29+
protected Field(String name, String type, Model model){
3030
this.name = name;
3131
this.columnName = Utils.camelCaseToUnderScore(name);
3232

@@ -96,8 +96,17 @@ else if (type.equalsIgnoreCase("geometry")){
9696
else{ //Single model
9797

9898
columnName = columnName + "_id";
99-
foreignKey = new ForeignKey(columnName, type);
10099
columnType = "bigint";
100+
101+
//Typically, when we have a model field we want to create a
102+
//foreign key to tie the field to the model. The only exception
103+
//is if the model field references the parent model - which is
104+
//very rare (most models don't have a model field with the same
105+
//type).
106+
if (!type.equals(model.getName())){
107+
foreignKey = new ForeignKey(columnName, type);
108+
}
109+
101110
}
102111

103112
}

Diff for: src/javaxt/orm/Model.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ protected Model(String modelName, JSONObject modelInfo, String packageName, Hash
7272
if (name.equalsIgnoreCase("id")) continue;
7373

7474
//Create field and update the fields array
75-
Field field = new Field(name, type);
75+
Field field = new Field(name, type, this);
7676
addConstraints(field, f.toJSONObject());
7777
this.fields.add(field);
7878
}
@@ -84,7 +84,7 @@ protected Model(String modelName, JSONObject modelInfo, String packageName, Hash
8484
if (hasMany!=null)
8585
for (int i=0; i<hasMany.length(); i++){
8686
JSONObject json = hasMany.get(i).toJSONObject();
87-
Field field = new Field(json.get("name").toString(), json.get("model").toString()+"[]");
87+
Field field = new Field(json.get("name").toString(), json.get("model").toString()+"[]", this);
8888
fields.add(field);
8989
}
9090

0 commit comments

Comments
 (0)