Skip to content

Instantly share code, notes, and snippets.

@vicapow
Last active December 14, 2015 21:18
Show Gist options
  • Save vicapow/5149683 to your computer and use it in GitHub Desktop.
Save vicapow/5149683 to your computer and use it in GitHub Desktop.
CREATE TABLE IF NOT EXISTS openclipart_issues(
clipart INTEGER NOT NULL
, reporter INTEGER DEFAULT NULL
, PRIMARY KEY(clipart, reporter)
, FOREIGN KEY(reporter) REFERENCES openclipart_users(id)
, FOREIGN KEY(clipart) REFERENCES openclipart_clipart(id)
) CHARACTER SET utf8 COLLATE utf8_general_ci ENGINE=InnoDB;
INSERT INTO openclipart_issues(clipart, reporter)
SELECT id, NULL
FROM ocal_files
WHERE upload_tags LIKE '%clipart_issue%';
-- ERROR 1452 (23000) at line 366: Cannot add or update a child row: a foreign key constraint fails (`ocal`.`openclipart_issues`, CONSTRAINT `openclipart_issues_ibfk_1` FOREIGN KEY (`reporter`) REFERENCES `openclipart_users` (`id`))
@willfong
Copy link

MariaDB [vicapow]> CREATE TABLE users ( id INT, UNIQUE (id) );
Query OK, 0 rows affected (0.02 sec)

MariaDB [vicapow]> INSERT INTO users ( id ) VALUES ( 1 );
Query OK, 1 row affected (0.01 sec)

MariaDB [vicapow]> CREATE TABLE issues ( clipart INT NOT NULL, reporter INT DEFAULT NULL, PRIMARY KEY (clipart, reporter), FOREIGN KEY (reporter) REFERENCES users (id) ) ENGINE=InnoDB;
Query OK, 0 rows affected (0.00 sec)

MariaDB [vicapow]> INSERT INTO issues ( clipart, reporter ) VALUES ( 1, 1 );
Query OK, 1 row affected (0.00 sec)

MariaDB [vicapow]> INSERT INTO issues ( clipart, reporter ) VALUES ( 1, NULL );
ERROR 1048 (23000): Column 'reporter' cannot be null
MariaDB [vicapow]> DROP TABLE issues;
Query OK, 0 rows affected (0.01 sec)

MariaDB [vicapow]> CREATE TABLE issues ( clipart INT NOT NULL, reporter INT DEFAULT NULL, UNIQUE KEY (clipart, reporter), FOREIGN KEY (reporter) REFERENCES users (id) ) ENGINE=InnoDB;
Query OK, 0 rows affected (0.01 sec)

MariaDB [vicapow]> INSERT INTO issues ( clipart, reporter ) VALUES ( 1, 1 );
Query OK, 1 row affected (0.00 sec)

MariaDB [vicapow]> INSERT INTO issues ( clipart, reporter ) VALUES ( 1, NULL );
Query OK, 1 row affected (0.00 sec)

MariaDB [vicapow]> INSERT INTO issues ( clipart, reporter ) VALUES ( 1, NULL );
Query OK, 1 row affected (0.00 sec)

MariaDB [vicapow]>

@vicapow
Copy link
Author

vicapow commented Mar 13, 2013

Cannot add or update a child row: a foreign key constraint fails (ocal.openclipart_issues, CONSTRAINT openclipart_issues_ibfk_1 FOREIGN KEY (clipart) REFERENCES openclipart_clipart (id))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment