views:

83

answers:

1

What is the best way to handle following situation?

A dropdown(for master table) is optional in a particular form. But, In database table the field is constrained with foreign key. If user don't select from dropdown then It creates problem because of foreign key.

One solution is to create default option in master table and use it in case of blank selection. but in dropdown, we need to handle this to show on top. Is it perfect solution? Is there any other optimized solution for this?

Thanks

+2  A: 

If it's required in the database, then it should not be optional on the front end. Either change the database, or change the front end. A foreign key constraint alone should not make this mandatory, you may want to make the field nullable.

Paddy
Can we insert null in foreign key constraint? Is null insertion good for DB?
Brij
It depends on your data structure - in this case, null would indicate that this item is not linked with whatever your FK is. Some people don't like null, but it's really down to you.
Paddy
If you genuinely can have an unlinked item, NULL is by far the best option for storing the fact that it's unlinked rather than any marker value. It's what the database does anyway when returning a left join so it's nice and consistent, and it's easy for the database to sense and handle with functions like COALESCE when you need to react to it in your app.
eftpotrm