0

I have an Android project that includes a local database written in SQLite. There exists an sqldelight folder which contains a .sq file for every table in the database (User.sq, Routine.sq ...). I am able to access/use the tables and procedures defined here within my project.

Now, I intent to add Foreign Key relations to the database. However, if I try something like FOREIGN KEY (this_table_fk) REFERENCES OtherTable(other_table_pk), the project will not build because the .sq file I'm in is not able to find the OtherTable table created from the CREATE TABLE statement in OtherTable.sq or, by extension, its column other_table_pk.

How can I allow the 2 different .sq files to "see" the tables generated by the other?

I tried putting the CREATE TABLE statements of ThisTable and OtherTable in the same .sq file, but I get mismatched input 'CREATE' expecting (<EOF>, IDENTIFIER, JAVADOC_COMMENT) on the second CREATE TABLE statement. I'd much rather have a solution that keeps each table in its own .sq file, though.

Thanks.

Basic showing of ThisTable.sq and OtherTable.sq

ThisTable.sq:

CREATE TABLE ThisTable (
      this_table_pk INTEGER PRIMARY KEY AUTOINCREMENT,
      name TEXT,
      location TEXT,
      start_date TEXT,
      end_date TEXT, 
      this_column_fk INTEGER,
      FOREIGN KEY (this_column_fk) REFERENCES OtherTable(other_column_pk)

);

OtherTable.sq:

CREATE TABLE OtherTable (
      other_table_pk INTEGER PRIMARY KEY AUTOINCREMENT,
      name TEXT,
      location TEXT,
      start_date TEXT,
      end_date TEXT);
  • Can you post the .sq files your talking about? It's hard to help without seeing more code. – parkgrrr Apr 04 '17 at 04:47
  • yeah I'll edit the OP. I'm not trying to make money with what I'm making or anything but I tend to think it good principle to abstract specific implementation. One minute. – Nathan Free Apr 04 '17 at 04:54
  • It's done, by the by. – Nathan Free Apr 04 '17 at 05:21
  • 1
    Do you have access to the create script or code for when the DB is created? My guess is that ThisTable is getting created before OtherTable – parkgrrr Apr 04 '17 at 05:28
  • That was a good hypothesis and it put me on the right track. The correct answer was in a project-specific discrepancy between the names of the tables before and after generation due to a quirk in how the Android Studio platform was being used. Thanks. – Nathan Free Apr 04 '17 at 06:18
  • I'm glad you got it figured out! – parkgrrr Apr 04 '17 at 06:25

1 Answers1

0

The problem was due to a misuse of the Android Studio platform. However, parkgrrr conjectured that it was based on an ordering of table creation, which led me to the correct answer.