-
Notifications
You must be signed in to change notification settings - Fork 12
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
Add database schema #7
Conversation
Bhaiyya do we need a team id field in the team model ? Cuz we are using name as the primary key in the team model.Should I make a new commit for that ? |
fix: Foreign Key relations due to incorrect arguments
@Meetesh-Saini I am trying to achieve a 2NF database schema |
|
Tortoise adds an ID of its own if no primary key is mentioned |
All of the Models without a primary key rely on the default from the ORM And I liked to remain high level and let the orm manage the exact details of relations |
About the team having a max of N users I tried two approaches DO $$
BEGIN
IF NOT EXISTS (
SELECT 1
FROM pg_constraint
WHERE conname = 'max_users_per_team_constraint'
) THEN
-- Add the constraint only if it doesn't exist
ALTER TABLE "user"
ADD CONSTRAINT max_users_per_team_constraint
CHECK ((SELECT COUNT(*) FROM "user" WHERE team_id = "user".team_id) <= 3);
END IF;
END $$; but that lead to
So I setup a trigger on the DB CREATE OR REPLACE FUNCTION check_max_users_per_team()
RETURNS TRIGGER AS $$
BEGIN
IF (
SELECT COUNT(*)
FROM "user"
WHERE team_id = NEW.team_id
) >= 3 THEN
RAISE EXCEPTION 'Cannot have more than 3 users in a team';
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
DROP TRIGGER IF EXISTS max_users_per_team_trigger ON "user";
CREATE TRIGGER max_users_per_team_trigger
BEFORE INSERT OR UPDATE ON "user"
FOR EACH ROW EXECUTE FUNCTION check_max_users_per_team(); |
I wrote 3 over there but I will just inject the actual value |
Now trying to create more than 3 Users for one team leads to an Error from the driver await User.create(user_name="345", team_id=2) Edit (after new commits): await User.create(name="345", team_id=2)
|
Now the question is,
|
fix: change column names to not contain model names
misc: get rid of Text primary key
All has been tested, now only missing annotation for image_config and constraint checking based on config value |
Currently the |
Adds normalised database models.