Skip to content

Commit

Permalink
Merge pull request #32 from vonloxley/reference_correct_table
Browse files Browse the repository at this point in the history
Wrong reference generation / PRAGMA foreign_keys not set
  • Loading branch information
pardom committed Apr 11, 2013
2 parents e4a50c1 + 23f1fea commit df9fb5b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/com/activeandroid/DatabaseHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,14 @@ public DatabaseHelper(Context context) {
// OVERRIDEN METHODS
//////////////////////////////////////////////////////////////////////////////////////

@Override
public void onOpen(SQLiteDatabase db) {
if (SQLiteUtils.FOREIGN_KEYS_SUPPORTED) {
db.execSQL("PRAGMA foreign_keys=ON;");
Log.i("Foreign Keys supported. Enabling foreign key features.");
}
};

@Override
public void onCreate(SQLiteDatabase db) {
if (SQLiteUtils.FOREIGN_KEYS_SUPPORTED) {
Expand Down
3 changes: 2 additions & 1 deletion src/com/activeandroid/util/SQLiteUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public static String createTableDefinition(TableInfo tableInfo) {
TextUtils.join(", ", definitions));
}

@SuppressWarnings("unchecked")
public static String createColumnDefinition(TableInfo tableInfo, Field field) {
String definition = null;

Expand Down Expand Up @@ -162,7 +163,7 @@ else if (ReflectionUtils.isSubclassOf(type, Enum.class)) {
}

if (FOREIGN_KEYS_SUPPORTED && ReflectionUtils.isModel(type)) {
definition += " REFERENCES " + tableInfo.getTableName() + "(Id)";
definition += " REFERENCES " + Cache.getTableInfo((Class<? extends Model>) type).getTableName() + "(Id)";
definition += " ON DELETE " + column.onDelete().toString().replace("_", " ");
definition += " ON UPDATE " + column.onUpdate().toString().replace("_", " ");
}
Expand Down

0 comments on commit df9fb5b

Please sign in to comment.