Insert with foreign keys enabled prevented in transaction

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Insert with foreign keys enabled prevented in transaction

J Decker
     I was trying to debug this set of commands, which is actually run on
two
    connections within the same process... but that detail apparently
doesn't matter.



    PRAGMA foreign_keys=on;

    create table `option4_name` (`name_id` char(36) NOT NULL,`name`
    varchar(255) NOT NULL default '' CONSTRAINT `name` UNIQUE);

    create table `option4_map` (`option_id` char(36) NOT
    NULL,`parent_option_id` char(36) NOT NULL default '0',`name_id` char(36)
    NOT NULL default '0',`description` tinytext,CONSTRAINT `parent_key2`
UNIQUE
    (`parent_option_id`,`name_id`), FOREIGN KEY  (`parent_option_id`)
    REFERENCES `option4_map`(`option_id`)ON UPDATE CASCADE ON DELETE
CASCADE,
    FOREIGN KEY  (`name_id`) REFERENCES `option4_name`(`name_id`)ON UPDATE
    CASCADE ON DELETE CASCADE);

    create table `option4_values` (`option_id` char(36) default '0',`string`
    varchar(100) default NULL,`segment` int(11) default 0,CONSTRAINT
`value_id`
    UNIQUE (`option_id`,`segment`)ON CONFLICT IGNORE, FOREIGN KEY
    (`option_id`) REFERENCES `option4_map`(`option_id`)ON UPDATE CASCADE ON
    DELETE CASCADE);select tbl_name,sql from sqlite_master where
type='table'
    and

    BEGIN TRANSACTION;

    insert into option4_name (name_id,name) values(
    '82093fa2-3d93-11e8-98aa-6e01a5d0577f','.' );

    insert into option4_map

(option_id,parent_option_id,name_id)values('00000000-0000-0000-0000-000000000000','00000000-0000-0000-0000-000000000000','82093fa2-3d93-11e8-98aa-6e01a5d0577f'
    );

    --SQLITE ERROR:Result of prepare failed? foreign key mismatch -
    "option4_map" referencing "option4_name" at char 185[] in [insert into
    option4_map

(option_id,parent_option_id,name_id)values('00000000-0000-0000-0000-000000000000','00000000-0000-0000-0000-000000000000','82093fa2-3d93-11e8-98aa-6e01a5d0577f'
    )]
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users
Reply | Threaded
Open this post in threaded view
|

Re: Insert with foreign keys enabled prevented in transaction

Clemens Ladisch
J Decker wrote:
> foreign key mismatch - "option4_map" referencing "option4_name"
>
>     create table `option4_name` (`name_id` char(36) NOT NULL,
>     ...
>     create table `option4_map` (...
>     FOREIGN KEY  (`name_id`) REFERENCES `option4_name`(`name_id`)

name_id in the parent table must be the primary key (or at least be unique).


Regards,
Clemens
_______________________________________________
sqlite-users mailing list
[hidden email]
http://mailinglists.sqlite.org/cgi-bin/mailman/listinfo/sqlite-users