Hi,
To link generic tables together, I’m used to using this type of relations.
By generic tables
understand : comments, statistics, issues, etc.
create table comments
(
id int not null primary key,
ref varchar(32) not null primary key,
ref_id int not null primary key,
content text not null,
);
add index `ref` (`ref`, `ref_id`)
create table articles
(
id int not null primary key,
content text not null,
);
create table products
(
id int not null primary key,
name varchar(128) not null,
);
The hasMany relation is done with ref
and ref_id
. The ref
field refer to model name.
Some websites said : this is a lack of performance, not a good practices, …
They recommends this style of relations:
create table comments
(
id int not null primary key,
content text not null,
);
create table articles
(
id int not null primary key,
content text not null,
);
create table article_comments
(
article_id int not null,
comment_id int not null,
);
create table products
(
id int not null primary key,
name varchar(128) not null,
);
create table product_comments
(
product_id int not null,
comment_id int not null,
);
This, required belongsToMany relations.
What do you think about that please ?
Thanks.