Jump to content

Problem creating Foreign Key


doubledee

Recommended Posts

I have a "friends" table that is used to keep track of Users' Friends.  In it are two fields ("requestor_approved" and "requestee_approved") that can only be one of three values: 0, 1, 2

 

I created a lookup table called "ternary" that is supposed to enforce those three values, but I cannot seem to create a Foreign Key Constraint, even though I have done it before on other tables...

 

Here are the details of each table...

 

 

friend

Field 			Type 		Null 	Default
id 			mediumint( 		No  	  	 
requestor 		mediumint( 		No  	  	 
requestee 		mediumint( 		No  	  	 
requestor_approved 	tinyint(1) 		No  	  	 
requestee_approved 	tinyint(1) 		No  	  	 


Indexes:
Keyname 		Type 	Cardinality 	Field
PRIMARY 		PRIMARY 2 	 	id
idx_requestor 		INDEX 	2  		requestor
idx_requestee 		INDEX 	2  		requestee
idx_requestee_approved 	INDEX 	2  		requestee_approved
idx_requestor_approved 	INDEX 	2  		requestor_approved

 

 

ternary

Field 	Type 		Null 	Default 	Comments
state 	tinyint(1) 		No  	  	 
value 	varchar(5) 		No 


Indexes:
Keyname 	Type 	Cardinality 	Field
PRIMARY 	PRIMARY 	3  	state

 

When I am in phpMyAdmin and go to add the Foreign Key COnstraint, the "ternary" table doesn't appear even though it seems like my data-types and everything else should match up.

 

What is going on??

 

Thanks,

 

 

Debbie

 

Link to comment
Share on other sites

Oh yeah, great point Barand.  The default mysql storage engine "myisam" simply ignores any constraints in your DDL.  Myisam doesn't support referential integrity, nor transactions.  If your tables are myisam that could explain why you don't have an option to set constraints in phpMyAdmin.

Link to comment
Share on other sites

also,ensure tables are InnoDB. MyIsam does not support foreign keys

 

You get the golden prize!!!

 

THANK YOU!!!

 

What is funny is that I have my "friend" table as Inno but left "ternary" as ISAM because I figured it wouldn't hurt since it was the look-up table itself and thus had no Foreign Key?!  (Guess it did matter!)

 

BTW, not that that is fixed...

 

Why is it that if I try this add...

INSERT INTO `doubledee`.`friend` (
`id` ,
`requestor` ,
`requestee` ,
`requestor_approved` ,
`requestee_approved`
)
VALUES (
'3', '19', '1', '', ''
);

 

where I am leaving out two required (i.e. NOT NULL) fields, the INSERT works but I get this in phpMyAdmin...

Inserted rows: 1
Warning: #1366 Incorrect integer value: '' for column 'requestor_approved' at row 1
Warning: #1366 Incorrect integer value: '' for column 'requestee_approved' at row 1

 

and it just INSERTS a 0 for each field I forgot to specify??

 

I would expect my INSERT to fail...

 

Thanks,

 

 

Debbie

 

 

 

Link to comment
Share on other sites

Give those tinyint fields default values of zero. Omit them from the insert query completely. This will set them to zero initially

 

I had left out defaults while I was debugging.

 

But back to my question...

 

Why does phpMyAdmin/MySQL put in "0" for those two fields when there were no values in my INSERT and there were no defaults set on the fields?

 

It appears to just grab the first value in the Lookup Table?  :-/

 

 

Debbie

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.