mysql - Foreign Key cyclic reference Dilemma -


consider simple situation in there 2 tables, named users , workgroups.

  1. a user's email address primary key in users table.
  2. a workgroup_id primary key in workgroup table.
  3. a user can create multiple workgroups.
  4. a user can part of 1 workgroup.

under given scenario need track user created workgroup.

what have done:

  1. have variable named workgroup_id in users table know workgroup user belongs to. foreign key workgroup_id in workgroup table.
  2. have variable named user_email in workgroup table track user created workgroup. foreign key user_email in users table.

the problem facing here results in cyclic reference between users , workgroups table. since cyclic references anywhere in programming big no no.

how solve this? there better design pattern missing here?

edit: whether "circular references big no no" or not, conceptually may not since there implementation non universal in different databases, still remain valid problem. aggravated case when have use orm, orm support database limits kind of database design can have.

you need allow @ least 1 of foreign keys null. allow create first row in table, leaving foreign key empty placeholder. after create corresponding row on other table, update foreign key in first row.

you decide ok permanent condition. if create first workgroup automatically before creating users, first workgroup doesn't have creator, leave set null.


Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -