外键一对多(postgresql,php,yii2)

Heloo,

I just beginner with yii2 and postgresql. I have problem like this:

First table:

tbl_printer (PK dept_id and id)

| dept_id | id | name |

Second table

tbl_printer_group (PK dept_id and id)

| dept_id | id | printer1 | printer2 | printer3 |

printer1, printer2, printer3 data/value from tbl_printer.id

I want to 'DISALLOWED to DELETE' tbl_printer IF printer (id) STILL IN USE at tbl_printer_group (printer1,printer2,printer3). If not wrong foreign key, one to one from parent to child, but this one to many.

How to solve this problem, I'm using php, yii2 and postgresql.

Thanks and sorry for my english

The foreign key is added through migration with help of addForeignKey() method:

$this->addForeignKey(
    'name_of_foreign_key_constraint',
    'products',
    'category_id',
    'products_categories',
    'id',
    'CASCADE',
    'CASCADE'
);

The last two options is for ON UPDATE and ON DELETE accordingly.

Most DBMS support these options: RESTRICT, CASCADE, NO ACTION, SET DEFAULT, SET NULL.

Note that you can specify arrays instead of category_id and id columns:

If there are multiple columns, separate them with commas or use an array.

Framework provides most used and needed methods for that, but remember that you can always execute custom SQL inside a migration with just writing:

$this->execute('YOUR CUSTOM SQL QUERY');