Hey,
I have two tables:
CREATE TABLE `images` (
`id` int(255) NOT NULL auto_increment,
`uid` int(255) NOT NULL default '0',
`dirid` int(255) NOT NULL default '0',
`name` varchar(32) NOT NULL default '',
`size` int(255) NOT NULL default '0',
`uploaded` int(20) NOT NULL default '0',
`views` int(255) NOT NULL default '0',
`approvedby` int(255) NOT NULL default '0',
`premium` tinyint(1) NOT NULL default '0',
`comment` text NOT NULL,
PRIMARY KEY (`id`),
KEY `uid` (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE `users` (
`id` int(255) NOT NULL auto_increment,
`username` varchar(32) NOT NULL default '',
`password` varchar(32) NOT NULL default '',
`user_level` int(1) NOT NULL default '0',
`email` varchar(60) NOT NULL default '',
`lastlogin` int(12) NOT NULL default '0',
`enabled` int(1) NOT NULL default '0',
`probend` int(255) NOT NULL default '0',
`spaceused` int(255) NOT NULL default '0',
`spacelimit` int(255) NOT NULL default '0',
`numimages` int(255) NOT NULL default '0',
`account_active` int(1) NOT NULL default '0',
`signup_date` int(12) NOT NULL default '0',
`homepage` varchar(100) NOT NULL default '',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
As you can see, there are keys set at certain points.
However, when I run the following query
ALTER TABLE `users` ADD FOREIGN KEY ( `id` ) REFERENCES `tblname`.`images` (
`uid`
) ON DELETE CASCADE ;
I get an error:
#1452 - Cannot add or update a child row: a foreign key constraint fails (`tblname/#sql-d5a_37e63`, CONSTRAINT `#sql-d5a_37e63_ibfk_1` FOREIGN KEY (`id`) REFERENCES `images` (`uid`) ON DELETE CASCADE)
And I have no idea why.
The table types are both InnoDB, and the field types are the same type and length.
Any ideas?