Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Foreign Key Placement to Prevent Conflicts #7

Open
rafaelsampa opened this issue Oct 24, 2024 · 0 comments
Open

Foreign Key Placement to Prevent Conflicts #7

rafaelsampa opened this issue Oct 24, 2024 · 0 comments

Comments

@rafaelsampa
Copy link
Owner

Description

Ensure that all foreign keys are added at the end of table creation using the ALTER TABLE statement. This practice helps avoid conflicts during the table creation process, ensuring smooth schema updates and reducing potential circular dependencies between tables.

Local

Arquivo db_creation.py

Possible Solution

-- Criar as tabelas sem foreign keys
CREATE TABLE Receita (
    Id INTEGER PRIMARY KEY AUTOINCREMENT,
    Nome TEXT NOT NULL,
    Tempo TEXT NOT NULL,
    Instruções TEXT NOT NULL
);

CREATE TABLE Utensilio (
    Id INTEGER PRIMARY KEY AUTOINCREMENT,
    Nome TEXT NOT NULL
);

CREATE TABLE Valores_Nutricionais (
    Id INTEGER PRIMARY KEY AUTOINCREMENT,
    Nome TEXT NOT NULL,
    Gordura REAL,
    Carboidrato REAL,
    Proteina REAL,
    Porção REAL,
    Unidade TEXT
);

CREATE TABLE Cliente (
    Id INTEGER PRIMARY KEY AUTOINCREMENT,
    Nome TEXT NOT NULL,
    Email TEXT NOT NULL
);

CREATE TABLE Favoritado (
    Id INTEGER PRIMARY KEY AUTOINCREMENT,
    Id_Receita INT,
    Id_Cliente INT
);

CREATE TABLE Ingrediente (
    Id INTEGER PRIMARY KEY AUTOINCREMENT,
    Id_Receita INT,
    Id_Val_Nutri INT,
    Quantidade REAL,
    Unidade TEXT
);

CREATE TABLE Receita_Utensilio (
    Id INTEGER PRIMARY KEY AUTOINCREMENT,
    Id_Receita INT,
    Id_Utensilio INT
);

-- Adicionar as foreign keys com ALTER TABLE
ALTER TABLE Favoritado
ADD CONSTRAINT fk_favoritado_receita
FOREIGN KEY (Id_Receita) REFERENCES Receita(Id);

ALTER TABLE Favoritado
ADD CONSTRAINT fk_favoritado_cliente
FOREIGN KEY (Id_Cliente) REFERENCES Cliente(Id);

ALTER TABLE Ingrediente
ADD CONSTRAINT fk_ingrediente_receita
FOREIGN KEY (Id_Receita) REFERENCES Receita(Id);

ALTER TABLE Ingrediente
ADD CONSTRAINT fk_ingrediente_valores_nutricionais
FOREIGN KEY (Id_Val_Nutri) REFERENCES Valores_Nutricionais(Id);

ALTER TABLE Receita_Utensilio
ADD CONSTRAINT fk_receita_utensilio_receita
FOREIGN KEY (Id_Receita) REFERENCES Receita(Id);

ALTER TABLE Receita_Utensilio
ADD CONSTRAINT fk_receita_utensilio_utensilio
FOREIGN KEY (Id_Utensilio) REFERENCES Utensilio(Id);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant