T
T
Taras Labiak2015-06-12 21:23:30
MySQL
Taras Labiak, 2015-06-12 21:23:30

Why are foreign keys not used to link tables?

I observed this not only in Yii, so the question is general.
I was writing a project on Yii 2 and suddenly discovered that there are no foreign keys in the database, i.e. the tables are not related. And this despite the fact that I explicitly wrote in the migrations REFERENCES table (id) ..!!
Why are relationships between tables not being used?
Are schemas parsed and redundant removed before being sent to MySQL?
If so, can this "parsing and cleaning" somehow be configured and turned off?
In general, I can create constraints for table links myself, the primary key will not change and the records in the tables, presumably, will not be deleted. What pitfalls await me? Or table links are not created so that there are no problems with migration?
yii-framework.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Puma Thailand, 2015-06-12
@kissarat

in migration create tables via createTable
and then add keys using addForeignKey

D
Dimon, 2015-07-10
@Glimor

A piece of code from the migration

public $table = 'item';
public function up() {
$columns = array(
'id' => 'pk',
'name' => 'string NOT NULL',
'category_id' => 'integer');

  $this->createTable($this->table, $columns, 'engine=INNODB');
  // Foreign Keys:
$this->addForeignKey('fk_' . $this->table . '_category_id', $this->table, 'category_id', 'item_category', 'id', 'CASCADE', 'CASCADE');
}
the item_category table must already be created

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question