3

In currently running version of my application, I do not have any constraint on the uniqueness of user's phone number. It means, one phone number can be linked with multiple accounts. Only unique constrained is user's email id.

scenario 1: User registers with email Id and phone number => we create a new user record in Database respecting uniqueness of email. scenario 2: User registers with only number => we create a new user record in Database with a unique dummy-email that I generate.

In both of these scenarios , I maintain the uniqueness on email but ignore any such constraint on phone-number.

Now I need to maintain uniqueness property on both email and phone-number. So, I need to merge both the user accounts (dummy-email and real email) that share the same phone-number.

The problem is that both of these accounts can have multiple references at multiple places in Database. What should be the approach to merge two accounts together?

edit: I'm using MongoDB at backend. It generates '_id' field for every document and uses it as a primary key for that document. So, this '_id' field is served as a foreign key for that user document to rest of the database.

one sample document in users collections where user has a verified email =>

{
    _id: ObjectId("5d443787f86f9a3dfa782a3c"),
    name: 'user name',
    email: 'VerifiedUserEmail@gmail.com',
    phone_number: '1234567890'
}

another sample document in users collections where user is having duplicate phone-number =>

{
    _id: ObjectId("5c9a1146c89b2d09740ccd17"),
    name: 'dummy user name',
    email: 'DummyUserEmail@dummy-emails.com',
    phone_number: '1234567890'
}
varshneyanmol
  • 247
  • 1
  • 5
  • Can you please show some sample data? It would help to understand what your primary and foreign keys are - do the reference tables have email address as a foreign key, for instance? – Neville Kuyt Aug 05 '19 at 11:44
  • @NevilleKuyt I have edited the question for little more clarity. – varshneyanmol Aug 05 '19 at 13:18

1 Answers1

0

There are really two things you can do here, either create a level of indirection (a mapping table which maps IDs together which you query at runtime), or you will have to update all the other places you reference that ID.

Personally I'd consider just doing the update, and deleting the old record. It will give you the opportunity to decide on a case-by-case how to merge the records. Simply updating the foreign key will likely create a lot of weird cartesians that you are not expecting.

Rob Conklin
  • 8,806
  • 1
  • 19
  • 23