If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > FK problem

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-18-11, 23:53
corbeeresearch corbeeresearch is offline
Registered User
 
Join Date: Aug 2010
Posts: 17
FK problem

Hi

This part of code
PHP Code:
`FK_user_projectFOREIGN KEY (`user_id`) REFERENCES `tbl_user` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT
Won't work in the following code.

PHP Code:
CREATE TABLE IF NOT EXISTS `tbl_issue`
(
`
idINTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
`
namevarchar(256NOT NULL,
`
descriptionvarchar(2000),
`
project_idINTEGER,
`
type_idINTEGER,
`
status_idINTEGER,
`
owner_idINTEGER,
`
requester_idINTEGER,
`
create_timeDATETIME,
`
create_user_idINTEGER,
`
update_timeDATETIME,
`
update_user_idINTEGER
ENGINE InnoDB
;
CREATE TABLE IF NOT EXISTS `tbl_user`
(
`
idINTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
`
emailVarchar(256NOT NULL,
`
usernameVarchar(256),
`
passwordVarchar(256),
`
last_login_timeDatetime,
`
create_timeDATETIME,
`
create_user_idINTEGER,
`
update_timeDATETIME,
`
update_user_idINTEGER
ENGINE InnoDB
;
CREATE TABLE IF NOT EXISTS `tbl_project_user_assignment`
(
`
project_idInt(11NOT NULL,
`
user_idInt(11NOT NULL,
`
create_timeDATETIME,
`
create_user_idINTEGER,
`
update_timeDATETIME,
`
update_user_idINTEGER,
PRIMARY KEY (`project_id`,`user_id`)
ENGINE InnoDB
;
-- 
The Relationships
ALTER TABLE 
`tbl_issueADD CONSTRAINT `FK_issue_projectFOREIGN KEY (`project_id`) REFERENCES `tbl_project` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT;
ALTER TABLE `tbl_issueADD CONSTRAINT `FK_issue_ownerFOREIGN KEY (`owner_id`) REFERENCES `tbl_user` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT;
ALTER TABLE `tbl_issueADD CONSTRAINT `FK_issue_requesterFOREIGN KEY (`requester_id`) REFERENCES `tbl_user` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT;
ALTER TABLE `tbl_project_user_assignmentADD CONSTRAINT `FK_project_userFOREIGN KEY (`project_id`) REFERENCES `tbl_project` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT;
ALTER TABLE `tbl_project_user_assignmentADD CONSTRAINT `FK_user_projectFOREIGN KEY (`user_id`) REFERENCES `tbl_user` (`id`) ON DELETE CASCADE ON UPDATE RESTRICT;
-- 
Insert some seed data so we can just begin using the database
INSERT INTO 
`tbl_user`
(`
email`, `username`, `password`)
VALUES
('test1@notanaddress.com','Test_User_One'MD5('test1')),
(
'test2@notanaddress.com','Test_User_Two'MD5('test2'))

Where did I go wrong?

Thanks
Reply With Quote
  #2 (permalink)  
Old 01-19-11, 03:51
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,535
i'm sorry, "won't work" is not a valid mysql error message

got something more substantial?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 01-19-11, 04:30
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 623
Can you also send us the CREATE table statement for tbl_project?
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #4 (permalink)  
Old 01-19-11, 04:45
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 623
I created a basic table:
Code:
CREATE TABLE IF NOT EXISTS tbl_project
(
id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
name varchar(255)
) ENGINE = InnoDB;
And reran your script and it appears to work. You may have forgotten to create this table in your own environment.
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
  #5 (permalink)  
Old 01-19-11, 06:34
corbeeresearch corbeeresearch is offline
Registered User
 
Join Date: Aug 2010
Posts: 17
Thanks, it's now working.

I deleted the whole database instead of emptying it, then redid the process. Strange though that it didn't accept the foreign key earlier.
Reply With Quote
  #6 (permalink)  
Old 01-19-11, 06:43
it-iss.com it-iss.com is offline
Registered User
 
Join Date: Sep 2009
Location: San Sebastian, Spain
Posts: 623
I is quite possible that there was a table existing in the database with the same name but different fields. The create table would only have happened if the table did not exist and not if the layout was different.
__________________
Ronan Cashell
Senior Oracle/MySQL DBA
http://www.it-iss.com
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On