I am developing a sample project between users and products. A user can sell multiple products and a product can be sold by multiple users. So I designed my db with a many to many relationship. Below are my tables:
create table `users` (
`id` bigint(20) not null auto_increment,
`first_name` varchar(100) not null,
`last_name` varchar(100) not null,
`email` varchar(100) not null,
`password` varchar(255) not null,
`phone_number` varchar(20),
`created_at` timestamp default CURRENT_TIMESTAMP,
`updated_at` timestamp default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
primary key(`id`),
unique(`email`)
);
create table `products` (
`id` bigint(20) not null auto_increment,
`name` varchar(255) not null,
primary key(`id`),
unique(`name`)
);
create table `users_products` (
`user_id` bigint(20) not null,
`product_id` bigint(20) not null,
`quantity`int not null,
`price` DECIMAL(5, 2) not null,
`description` text not null,
`created_at` timestamp default current_timestamp,
`updated_at` timestamp default current_timestamp on update current_timestamp,
primary key (`user_id`, `product_id`),
foreign key(`user_id`) references `users`(`id`) on delete cascade,
foreign key(`product_id`) references `products`(`id`) on delete restrict
);
As you can see above, the products
table name is unique because users can sell the same products.
Now I ask the question how this can be achieved using spring boot with JPA. I have seen tutorials with many to many with extra columns but the tutorials have just had an extra column with date time. But I want to perssit with the extra columns as mentioned above in the users_products
table.
And also is the schema designed ok?