我有几个表:ingredients
、customers
、recipes
和。menus
restaurants
条件是:ingredients
HABTM customers
、recipes
HABTM ingredients
、menus
hasmanyrecipes
和restaurants
hasmany menus
。
但是,当我尝试使用 cmd 控制台全部烘焙蛋糕时,自动创建的关系就像配料有许多客户_配料,客户有许多客户_配料和客户_配料属于客户和配料,而不是给配料有和属于许多客户。怎么了?表定义还是控制台?
在这里,我列出了我的创建表语句:
CREATE TABLE customers (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
cust_gname VARCHAR(20) NOT NULL,
cust_fname VARCHAR(30) NOT NULL,
cust_street VARCHAR(30) NOT NULL,
cust_suburb VARCHAR(30) NOT NULL,
cust_state VARCHAR(6) NOT NULL,
cust_postcode VARCHAR(4) NOT NULL,
cust_email VARCHAR(50) NOT NULL,
cust_phone VARCHAR(12),
cust_mobile VARCHAR(12)
);
CREATE TABLE restaurants (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30),
address1 VARCHAR(30),
address2 VARCHAR(30),
suburb VARCHAR(30),
state VARCHAR(10),
postcode VARCHAR(4)
);
CREATE TABLE ingredients (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30)
);
CREATE TABLE menus (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
description VARCHAR(30),
restaurant_id INT UNSIGNED,
CONSTRAINT fk_restID FOREIGN KEY (restaurant_id) REFERENCES restaurants(id)
);
CREATE TABLE recipes (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(30),
menu_id INT UNSIGNED,
image VARCHAR(30),
CONSTRAINT fk_menuID FOREIGN KEY (menu_id) REFERENCES menus(id)
);
CREATE TABLE ingredient_recipes (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
ingredient_id INT UNSIGNED,
recipe_id INT UNSIGNED,
CONSTRAINT fk_ingID FOREIGN KEY (ingredient_id) REFERENCES ingredients(id),
CONSTRAINT fk_recID FOREIGN KEY (recipe_id) REFERENCES recipes(id)
);
CREATE TABLE customer_ingredients (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
customer_id INT UNSIGNED,
ingredient_id INT UNSIGNED,
CONSTRAINT fk_ingrID FOREIGN KEY (ingredient_id) REFERENCES ingredients(id),
CONSTRAINT fk_cusID FOREIGN KEY (customer_id) REFERENCES customers(id)
);