Clave Externa SQLite


Estoy siguiendo las instrucciones de la documentación de SQLite en http://www.sqlite.org/foreignkeys.html sin embargo, mi intento de agregar una clave foránea está fallando. Aquí están mis declaraciones create:

CREATE TABLE 
    checklist (
        _id INTEGER PRIMARY KEY AUTOINCREMENT, 
        checklist_title TEXT,
        description TEXT,
        created_on INTEGER, 
        modified_on INTEGER
    );

CREATE TABLE 
    item (
        _id INTEGER PRIMARY KEY AUTOINCREMENT,  
        FOREIGN KEY(checklist_id) REFERENCES checklist(_id), 
        item_text TEXT, item_hint TEXT, 
        item_order INTEGER, 
        created_on INTEGER, 
        modified_on INTEGER
    );

La primera tabla está hecha bien. El error ocurre en la segunda instrucción. He intentado tanto con envolver las dos consultas en una transacción y sin. Aquí está el error:

Columna desconocida "checklist_id" en la definición de clave foránea (código 1):, mientras compila: CREATE ITEM (_id INTEGER PRIMARY KEY AUTOINCREMENT, FOREIGN KEY(checklist_id) REFERENCES checklist(_id), item_text TEXT, item_hint TEXT, item_order INTEGER, created_on INTEGER, modified_on INTEGER)

Author: Geeks On Hugs, 2012-07-18

4 answers

Todavía tiene que crear la columna antes de agregarla como clave foránea.

Así que sería:

CREATE TABLE 
    checklist (
        _id INTEGER PRIMARY KEY AUTOINCREMENT, 
        checklist_title TEXT,
        description TEXT,
        created_on INTEGER, 
        modified_on INTEGER
    );

CREATE TABLE 
    item (
        _id INTEGER PRIMARY KEY AUTOINCREMENT,  
        checklist_id INTEGER,
        item_text TEXT, 
        item_hint TEXT, 
        item_order INTEGER, 
        created_on INTEGER, 
        modified_on INTEGER,
        FOREIGN KEY(checklist_id) REFERENCES checklist(_id)
    );
 71
Author: Scen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-07-18 17:25:33

Simplemente le falta la columna checklist_id en su tabla item. Necesitas declararlo antes de que quieras configurarlo como FOREIGN KEY. Ha intentado crear FK en una columna inexistente y esta es la razón por la que no funciona.

Así que necesitas agregar esto:

checklist_id INTEGER,
FOREIGN KEY(checklist_id) REFERENCES checklist(_id)

Ahora debería funcionar.

 4
Author: Simon Dorociak,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-07-18 15:55:12

Debe incluir el nombre de la columna antes de envolverla con la CLAVE FORÁNEA().

CREATE TABLE 
    item (
        _id INTEGER PRIMARY KEY AUTOINCREMENT,  
        checklist_id INTEGER,
        FOREIGN KEY(checklist_id) REFERENCES checklist(_id), 
        item_text TEXT, item_hint TEXT, 
        item_order INTEGER, 
        created_on INTEGER, 
        modified_on INTEGER
    );
 3
Author: lorraine,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-07-18 15:49:03

Ponga la definición de CLAVE FORÁNEA al final de la instrucción SQL

 2
Author: rudakovsky,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-08-05 13:14:26