0
votes

I'm designing the DB for the first project with Cake and having problem with duplicated foreign key constraint.

I create the db model in mysql workbench, something like

student table id/name/

studentProfile table id/profile_value/student_id Foreign Key: (**student_id**) Ref student.id

student_class table student_id class_id Foreign Key: (**student_id**) ref student.id

class table id/className

I got error#121 when insert into mysql.After reading, it's caused by the duplicated foreign key. (http://thenoyes.com/littlenoise/?p=81)

But as stated in cakephp's doc, foreign key = name of the related table followed by _id. So if there're multiple foreign keys pointing to the same field, there will be duplications.

Foreign keys in hasMany, belongsTo or hasOne relationships are recognized by default as the (singular) name of the related table followed by _id. So if a Baker hasMany Cake, the cakes table will refer to the bakers table via a baker_id foreign key. For a multiple worded table like category_types, the foreign key would be category_type_id.

Any suggestions?

1
Your tables should be called 'students', 'student_profiles', 'student_classes' & 'classes' unless you are intentionally overriding CakePHP's naming conventions. - RichardAtHome
right, that's one problem, but it's not the cause of the error121. any suggestion? - MrHulala
@RichardAtHome classes_students* - AD7six
I got error#121 when insert into mysql please show some code and the exact error message demonstrating you doing that - that sql error is related to creating tables, which CakePHP doesn't do for you when creating data. - AD7six

1 Answers

-2
votes

plz use alias when you call same name FK

studentProfile.student_id and student_class.student_id