views:

301

answers:

1

I'm new to CakePHP, and I'm trying to decide between the schema shell that comes with CakePHP and the migrations tools that were written by Joel Moss and Georgi Momchilov.

So far I haven't found any direct support in either of them for creating foreign key constraints. It appears that you might be able to write raw SQL in a migration to create a foreign key.

Have I missed something? Is there support somewhere? Should I even be using foreign keys? I've seen a few comments related to CakePHP or Ruby on Rails saying that foreign key constraints are discouraged. Our database is only used by one application.

+2  A: 

I wouldn't say that they're discouraged, per se. With both CakePHP and Rails, the data integrity that foreign keys offer is handled by the framework. That said, it's only true if you do everything the "right" way. If you're writing your own SQL, operating in a non-standard manner or accessing/manipulating the data through any other means, then foreign keys are definitely a good idea, in my opinion.

I've never used migration tools for CakePHP, but in my limited Rails work, I've always applied the keys in the migration using SQL. That way you get all of the inherent benefits of both. You have your migration and you get your domain constraints in case you ever skirt the framework to achieve some particular result.

Rob Wilkerson
This is what I ended up doing: migrations with custom SQL to create the foreign keys constraints.
Don Kirkby