Suppose there is a two tables "User" and "Product", so there is also a foreign key, that means, that one user can have multiple products, like:
CREATE TABLE "user" (
id BIGSERIAL NOT NULL,
Name VARCHAR(100) NOT NULL,
ProductId PRIMARY KEY,
Products FOREIGN KEY(ProductId) REFERENCES product(id)
)
CREATE TABLE "product"(
id BIGSERIAL NOT NULL PRIMARY KEY,
product_name VARCHAR(100) NOT NULL,
)
The problem is when I'm creating a new user I don't specify ProductId, (because for example product does not exist yet) it always it throws an exception, that ProductId has a unique constraint and should be not null.
So What I Need to Put Inside ProductId?
Because I don't specify any value for it when creating User, it throws unique constraint error, but without this Field I cannot have a foreign key between two tables, so it cannot be removed.
So, the question is "How can I Specify Default Value for This Reference "ProductId" Field in My "User" model, when creating an user object, in order to avoid this unique constraint error."
productidas the primary key for a table namedusersounds extremely weird to me. Why isn'tuser.idthe primary key? And why is a user only related to a single product? That sounds like a strange decision to me as well.to make "one object" have a lot of another's:)