Thanks for your answer, Oliver.

> http://www.sqlite.org/foreignkeys.html
Yes, I have already read this before mailing here.

> PRAGMA foreign_keys
Yes, I knew about this PRAGMA.

Ok, I'll try to describe my problem with one example. Imagine we have
to store information about pages and the keywords. Any page may
contain any number of keywords, but any keyword may also belong to any
number of pages. So I'm trying to implement many-to-many relationship.

Here is small copy and paste from my terminal:

SQLite version 3.7.0.1
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> PRAGMA foreign_keys = ON;
sqlite> CREATE TABLE pages (
   ...> address VARCHAR ( 128 ) DEFAULT '/',
   ...> body TEXT DEFAULT ''
   ...> );
sqlite> CREATE TABLE keywords ( word VARCHAR ( 64 ) NOT NULL UNIQUE );
sqlite> CREATE TABLE relations (
   ...> page INTEGER NOT NULL,
   ...> keyword INTEGER NOT NULL,
   ...> FOREIGN KEY ( page ) REFERENCES pages( rowid ) ON UPDATE
CASCADE ON DELETE CASCADE,
   ...> FOREIGN KEY ( keyword ) REFERENCES keywords( rowid ) ON UPDATE
CASCADE ON DELETE CASCADE
   ...> );
sqlite> INSERT INTO pages ( body ) VALUES ( '' );
sqlite> INSERT INTO pages ( address ) VALUES ( '/contacts' );
sqlite> INSERT INTO keywords ( word ) VALUES ( 'word1' );
sqlite> INSERT INTO keywords ( word ) VALUES ( 'word2' );

As for this place all were ok and here is the problem begin:

sqlite> INSERT INTO relations VALUES ( 1,2 );
Error: foreign key mismatch
sqlite> INSERT INTO relations VALUES ( 1,1 );
Error: foreign key mismatch

Can anyone please tell what I did wrong?
_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to