SQLite Forum

Bug?: CREATE TABLE with unsatisfied FK and DROP TABLE results in Error
Login
I like to give a little more information here.

Again the complete script which FAILS (and it should not):

```
➜  sqlite-tools-osx-x86-3310100 cat script.sql                              
PRAGMA foreign_keys;

CREATE TABLE "table" (
  "id"              INTEGER PRIMARY KEY AUTOINCREMENT
);

CREATE TABLE "column" (
  "id"              INTEGER PRIMARY KEY AUTOINCREMENT,

  "other_table_id"  INTEGER,
  "other_column_id" INTEGER,

  FOREIGN KEY (other_table_id) REFERENCES "table" (id) ON DELETE CASCADE ON UPDATE CASCADE
  ,FOREIGN KEY (other_column_id) REFERENCES "column" (id) ON DELETE CASCADE ON UPDATE CASCADE
);

DROP TABLE "table";
DROP TABLE "column";
➜  sqlite-tools-osx-x86-3310100 rm test.db && ./sqlite3 test.db < script.sql
foreign_keys
------------
1           
Error: near line 18: no such table: main.table

```

**The FK to "table" is  in the way when dropping "column".**

If we change the script to not include the self-referencing foreign key, DROPPING the tables work. So  I am very sure that this behaviour is a bug.

So here, we have SUCCESS, script does not fail.

```
➜  sqlite-tools-osx-x86-3310100 rm test.db && ./sqlite3 test.db < script.sql
foreign_keys
------------
1           
➜  sqlite-tools-osx-x86-3310100 cat script.sql
PRAGMA foreign_keys;

CREATE TABLE "table" (
  "id"              INTEGER PRIMARY KEY AUTOINCREMENT
);

CREATE TABLE "column" (
  "id"              INTEGER PRIMARY KEY AUTOINCREMENT,

  "other_table_id"  INTEGER,
  "other_column_id" INTEGER,

  FOREIGN KEY (other_table_id) REFERENCES "table" (id) ON DELETE CASCADE ON UPDATE CASCADE
  -- ,FOREIGN KEY (other_column_id) REFERENCES "column" (id) ON DELETE CASCADE ON UPDATE CASCADE
);

DROP TABLE "table";
DROP TABLE "column";
➜  sqlite-tools-osx-x86-3310100 rm test.db && ./sqlite3 test.db < script.sql
foreign_keys
------------
1           
```


**The FK to "table" is not in the way when dropping "column".**