This is the context. I have a databasehelper that creates 3 tables in one database. Two of those tables have foreign keys. Everything worked it was all good but given what I wanted to do I needed to use listviews instead of textviews to display my data and be able to edit, delete etc. For some reason listviews are a different animal on Android than other programming languages. I figured out adapters and all that nonsense. My new problem was that My app wouldn't display the data and when I checked my logcat I got this message
Caused by: java.lang.IllegalArgumentException: column '_id' does not exist. Available columns: [id, termName, termStart, termEnd]
That was for one table named 'term_table'. So I changed the id from 'id' to '_id' and that did the trick.
So everything is all good right? Nope, because the other two tables have their own ID's and foreign keys. When I did the same query for a different table named 'assess_table' I got the same error so I change the 'id' to '_id'. The problem is 'courses_table' (third table) uses '_id' as a foreign key to reference a table called 'term_table'. So I get an error telling me I was creating duplicate columns- the primary key '_id' and the foreign key '_id' to reference term_table.
I rename the id of 'courses_table' from '_id' to 'courses_id'. Now I'm back to square one getting the same error:
Caused by: java.lang.IllegalArgumentException: column '_id' does not exist. Available columns: [assess_id, assessName, assessDueDate, assessType, courses_id]
So basically if all three tables have to be named _id what syntax am I supposed to use when creating foreign keys if the foreign keys have the same name as the primary key of the referenced table, so I'll always get the creating duplicate columns error. I read that this _id naming scheme is only a thing with cursor adapters. What syntax is appropriate to use?
This is my 3 tables in my databasehelper
//terms table
public static final String TERM_TABLE_NAME = "term_table";
public static final String COL_TERM1 = "_id";
public static final String COL_TERM2 = "termName";
public static final String COL_TERM3 = "termStart";
public static final String COL_TERM4 = "termEnd";
public static final String TERM_DATABASE_NAME = "term.db";
//courses table
public static final String COURSES_TABLE_NAME = "courses_table";
public static final String COL_COURSE1 = "courses_id";
public static final String COL_COURSE2 = "courseTitle";
public static final String COL_COURSE3 = "courseStartDate";
public static final String COL_COURSE4 = "courseEndDate";
public static final String COL_COURSE5 = "courseStatus";
public static final String COL_COURSE6 = "optionalNote";
public static final String COL_COURSE7 = "mentorName";
public static final String COL_COURSE8 = "mentorPhone";
public static final String COL_COURSE9 = "mentorEmail";
public static final String COL_COURSE_TERMID = "_id";
//assess table
public static final String ASSESS_TABLE_NAME = "assess_table";
public static final String COL_ASSESS1 = "assess_id";
public static final String COL_ASSESS2 = "assessName";
public static final String COL_ASSESS3 = "assessDueDate";
public static final String COL_ASSESS4 = "assessType";
public static final String COL_ASSESS_COURSEID = "courses_id";
this is the create syntax
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TERM_TABLE_NAME + " (_id INTEGER PRIMARY KEY AUTOINCREMENT, termName TEXT, termStart TEXT, termEnd TEXT)");
db.execSQL("CREATE TABLE " + COURSES_TABLE_NAME + " (courses_id INTEGER PRIMARY KEY AUTOINCREMENT, courseTitle TEXT, courseStartDate TEXT, courseEndDate TEXT, courseStatus INTEGER, optionalNote TEXT, mentorName TEXT, mentorPhone TEXT, mentorEmail TEXT, _id INTEGER, FOREIGN KEY (_id) REFERENCES term_table(_id))");
db.execSQL("CREATE TABLE " + ASSESS_TABLE_NAME + " (assess_id INTEGER PRIMARY KEY AUTOINCREMENT, assessName TEXT, assessDueDate TEXT, assessType INTEGER, courses_id INTEGER, FOREIGN KEY (courses_id) REFERENCES courses_table(courses_id))");
}