Register data in a foreign key table

4

I'm trying to register a user but in his table there is a foreign key attached to another table.

My problem is that this foreign key is referencing the address table, but I do not yet have an address for this user, I want it to be able to register later when you want.

What can I do at the time of registration? I already tried to leave the default value as null and 0 but it did not work.

    
asked by anonymous 25.03.2016 / 08:21

2 answers

1

Does your bank need to have such a relationship?

Look, the most correct would be for you to have a relationship from Users to Addresses 1:N , that is, a User might have more than one Address . >

This way you would not have to enter an Address Id in the Users table.

But if this should be the way you are creating then I suppose you have an address table already registered in your bank (Post Office) for example, it would still be a bad relationship, would not have the street number or how to enter another street address if necessary.

If you really need to create your bank the way you are speaking then do not use the address Id set to not null .

    
25.03.2016 / 12:53
2

Peter, this is happening because your foreign key by default is set to NOT NULL .

What you should do is change the foreign key column to accept NULL . Since I'm not aware of your table, it would look something like this.

ALTER TABLE usuario MODIFY endereco_id INT NULL;

I'm not sure why I'm not with the MySQL environment here, but maybe you have to drop the contraint of your foreign key, modify it to accept NULL, and then add the constraint again.

    
25.03.2016 / 09:06