Author Topic: How to set foreign key in SQLite  (Read 202 times)

johnaaronrose

  • Newbie
  • *
  • Posts: 5
How to set foreign key in SQLite
« on: January 10, 2012, 10:10:34 AM »
I've looked at the Database examples in Gambas2. However, I don't see how to set a field as a foreign key when creating a table. I'm able to set a field as a table's primary key using:
JHTable.PrimaryKey = ["_id"]
after:
JHTable = databaseConnection.Tables.Add("country")
JHTable.Fields.Add("_id", db.Serial)

I expected after keying in "JHTable." that ForeignKey would come up as an option for selection but it didn't.

Any ideas please?
Regards,
John

Linux Basic

How to set foreign key in SQLite
« on: January 10, 2012, 10:10:34 AM »

johnaaronrose

  • Newbie
  • *
  • Posts: 5
Re: How to set foreign key in SQLite
« Reply #1 on: January 16, 2012, 11:47:18 AM »
There no support for Foreign Keys in gb.db & gb.sqlite3 components: so the method that I previously tried is not available.

I didn't realise that a table could be created using the Exec() method. I've done that (including the Foreign Key clause) and it worked! Works in Gambas2 & Gambas3.

PS Unfortunately, sqlite3 does not allow "ALTER TABLE ADD CONSTRAINT" to add the foreign key: I've tried it & the statement was rejected with a syntax error. Further confirmation of this can be found on:http://stackoverflow.com/questions/1884818/how-do-i-add-a-foreign-key-to-an-existing-sqlite-3-6-21-table
Regards,
John