forked from despensa-app/despensa-rest-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schema.sql
147 lines (132 loc) · 6.26 KB
/
schema.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
-- MySQL Script generated by MySQL Workbench
-- Sun Jul 7 19:01:30 2024
-- Model: New Model Version: 1.0
-- MySQL Workbench Forward Engineering
SET @OLD_UNIQUE_CHECKS = @@UNIQUE_CHECKS, UNIQUE_CHECKS = 0;
SET @OLD_FOREIGN_KEY_CHECKS = @@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS = 0;
SET @OLD_SQL_MODE = @@SQL_MODE, SQL_MODE =
'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
-- -----------------------------------------------------
-- Schema despensa_app
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Table `products`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `products`
(
`id` BIGINT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(255) NOT NULL COMMENT 'Nombre del producto',
`price` DECIMAL(11, 2) UNSIGNED NOT NULL COMMENT 'Precio del producto',
`img_url` VARCHAR(255) NOT NULL COMMENT 'Url de la imagen del producto.',
`calories` DECIMAL(11, 2) UNSIGNED NOT NULL COMMENT 'Calorías del producto',
`description` VARCHAR(255) NULL COMMENT 'Descripción del producto, notas, etc.',
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
)
ENGINE = InnoDB
COMMENT = 'Productos';
-- -----------------------------------------------------
-- Table `users`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `users`
(
`id` BIGINT NOT NULL AUTO_INCREMENT,
`username` VARCHAR(45) NOT NULL,
`password` VARCHAR(60) NOT NULL,
`email` VARCHAR(100) NULL,
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE INDEX `email_UNIQUE` (`email` ASC) VISIBLE,
UNIQUE INDEX `username_UNIQUE` (`username` ASC) VISIBLE
)
ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `shopping_list`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `shopping_list`
(
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(250) NULL,
`total_products` INT UNSIGNED NOT NULL DEFAULT 0 COMMENT 'Número total de productos.',
`total_calories` DECIMAL(11, 2) UNSIGNED NOT NULL DEFAULT 0 COMMENT 'Total de calorías de todos los productos.',
`total_price` DECIMAL(11, 2) UNSIGNED NOT NULL DEFAULT 0 COMMENT 'Precio total de los productos.',
`user_id` BIGINT NOT NULL,
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
INDEX `fk_shopping_list_users1_idx` (`user_id` ASC) VISIBLE,
CONSTRAINT `fk_shopping_list_users1`
FOREIGN KEY (`user_id`)
REFERENCES `users` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
)
ENGINE = InnoDB
COMMENT = 'Lista de la compra';
-- -----------------------------------------------------
-- Table `unit_types`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `unit_types`
(
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(50) NOT NULL,
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
)
ENGINE = InnoDB
COMMENT = 'Tipo de unidad del producto (packs, bricks, botellas, kilos, gramos, latas)';
-- -----------------------------------------------------
-- Table `products_has_shopping_list`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `products_has_shopping_list`
(
`product_id` BIGINT NOT NULL,
`shopping_list_id` INT NOT NULL,
`unit_type_id` INT NOT NULL,
`units_per_product` INT UNSIGNED NOT NULL DEFAULT 1 COMMENT 'Unidades por producto.',
`total_calories` DECIMAL(11, 2) UNSIGNED NOT NULL COMMENT 'Total de calorías por unidad.',
`total_price` DECIMAL(11, 2) UNSIGNED NOT NULL COMMENT 'Precio total por unidad.',
`selected` TINYINT(1) NOT NULL DEFAULT 0 COMMENT 'Establece si el producto esta seleccionado.',
PRIMARY KEY (`product_id`, `shopping_list_id`, `unit_type_id`),
INDEX `fk_products_has_shopping_list_shopping_list_id` (`shopping_list_id` ASC) VISIBLE,
INDEX `fk_products_has_shopping_list_products_id` (`product_id` ASC) VISIBLE,
INDEX `fk_products_has_shopping_list_unit_types_id` (`unit_type_id` ASC) VISIBLE,
CONSTRAINT `fk_products_has_shopping_list_products`
FOREIGN KEY (`product_id`)
REFERENCES `products` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_products_has_shopping_list_shopping_list`
FOREIGN KEY (`shopping_list_id`)
REFERENCES `shopping_list` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_products_has_shopping_list_unit_types`
FOREIGN KEY (`unit_type_id`)
REFERENCES `unit_types` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION
)
ENGINE = InnoDB
COMMENT = 'Relación de productos y lista de la compra.';
-- -----------------------------------------------------
-- Table `settings`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS `settings`
(
`id` INT NOT NULL AUTO_INCREMENT,
`key_name` VARCHAR(45) NOT NULL,
`value` VARCHAR(255) NOT NULL,
`description` TEXT NULL,
`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE INDEX `key_name_UNIQUE` (`key_name` ASC) VISIBLE
)
ENGINE = InnoDB;
SET SQL_MODE = @OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS = @OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS = @OLD_UNIQUE_CHECKS;