Difference between #1 and #2 of
Guidelines for good schema design

Revision #2 has been created by Maurizio Domba Cerin on Aug 27, 2011, 8:26:53 PM with the memo:

doc fix
« previous (#1) next (#3) »


Title unchanged

Guidelines for good schema design

Category unchanged


Yii version unchanged

Tags unchanged

database, schema design

Content changed

But even without Yii considering them, foreign-keys are a vital part of maintaining referential integrity of your database; there are many tutorials on the web on how to learn about them.

DO name your foreign key fields ending in "id"
Related to the prior note, if
you have a field that holds the ID of a user, call the field `userid` rather than `user`. This is because for every foreign key you include in a table, you'll almost certainly wish to define a **relation** for it.

In Yii, class variables, db fields, virtual attributes, relations all share a single namespace, so it's not possible to have `$model->user` be **both** the foreign key in the table **and** the relation.

By calling the FK `userid`, the BELONGS_TO relation of `$model->user` forms naturally and easily:
122 0
Viewed: 91 538 times
Version: 1.1
Category: Tips
Written by: Steve Friedl
Last updated by: softark
Created on: Aug 27, 2011
Last updated: 5 years ago
Update Article


View all history