1

Here is the scenario:

Let's assume that we have a Telephone and a Driver.

Here is the definition of the classes (Pseudocode):

Telephone:

{
  Id: number,
  [Unique] TelephoneNumber: string
}

Driver:

{
  Id: number,
  Name: string
}

Telephone Number has a unique constraint.

The driver has a relationship with the telephone.

What's my problem here: My boss says that I should use TelephoneNumber in the Driver class to make the relationship with Telephone. But usually, it has been done by TelephoneId rather than unique constraint.

Is it the correct approach to use unique keys for relationship rather than Id's?

1
  • May be, your boss is saying to add a reference to telephone class in driver class. Something like db.Driver.TelephoneId, where is the entity framework context. Dec 31, 2021 at 4:21

2 Answers 2

3

Lots of considerations here.

  • Do you have a pool of numbers (e.g. cell) that get allocated to drivers? Maybe ID is right.
  • Is the number just a number for the driver? Should it be just data against the driver with few constraints and not an FK at all?
  • Do drivers have multiple phone numbers?
  • Do you need the number to be stored one way & formatted another? ID might be better.

So as always, it depends. Nobody on SE can really answer that for you without knowing the business domain.

0

Generally speaking, if you only have two fields for a table, and one is Id, and the other is unique. Then you don't need the ID.

With you example of a driver with multiple phone numbers the technical solution I would go with would be:

Driver
  Id
  Name
  Otherinfo

TelephoneDriverLinkingTable
  TelephoneNumber
  DriverId
  StartDate
  EndDate

Then you can have an object

Driver
{
   Id: 'some guid',
   TelephoneNumbers: []
   Name : 'bob'
}

You might need to fiddle around a bit to include international dialling codes to ensure uniqueness.

However there is also the data protection concern. A telephone number is personal information it will be problematic to use it in logs or say request fields.

Adding an additional unrelated ID and using that instead of the natural key might be preferable in the case of personal-information-which-is-also-a-natural-key depending on how you are using that key

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.