# Circular foreign keys and order of creation

**URL:** <https://www.ruby-forum.com/t/circular-foreign-keys-and-order-of-creation/108236>\
**Category:** Rails\
**Created:** [June 30, 2007, 8:13pm UTC](https://www.ruby-forum.com/t/circular-foreign-keys-and-order-of-creation/108236 "2007-06-30T20:13:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [June 30, 2007, 8:13pm UTC](https://www.ruby-forum.com/t/circular-foreign-keys-and-order-of-creation/108236/1 "2007-06-30T20:13:53Z")

</div>

Hi,

I have two database tables

accounts  
id  
owner\_id

users  
id  
account\_id

An account must have an owner\_id which is a user’s id. So the owner\_id  
field must not be null. A user must have an account\_id and so  
account\_id must not be null also. I create the account first but then  
the owner\_id has to be null at least until the user is created and I  
know the user’s id. This is a problem like the chicken and the egg.

I searched google but wasn’t able to find how to handle this problem  
in a Rails app.

One approach is I can allow owner\_id to be null and when creating the  
account I can create the account, create the user with the account\_id,  
and then set the owner\_id on the account. Then check that all went  
well. This whole dance happens in the Ruby of my Account model. This  
doesn’t seem like fully safe approach. Is there something better?

I’m using PostgreSQL but am hoping there is a database agnostic  
solution.

Any suggestions?

Thanks,  
Peter

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [June 30, 2007, 8:33pm UTC](https://www.ruby-forum.com/t/circular-foreign-keys-and-order-of-creation/108236/2 "2007-06-30T20:33:54Z")

</div>

Peter M. wrote the following on 30.06.2007 20:12 :

> account\_id  
> One approach is I can allow owner\_id to be null and when creating the  
> account I can create the account, create the user with the account\_id,  
> and then set the owner\_id on the account. Then check that all went  
> well. This whole dance happens in the Ruby of my Account model. This  
> doesn’t seem like fully safe approach. Is there something better?
> 
> I’m using PostgreSQL but am hoping there is a database agnostic solution.

This has nothing to do with RoR…  
Just drop one of the key. Only one is needed to link your objects,  
trying to have both refer to the other is duplicating information.

For RoR specifics on one-to-one relationships, look at the documentation  
of has\_one and belongs\_to.

Lionel.

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [June 30, 2007, 8:48pm UTC](https://www.ruby-forum.com/t/circular-foreign-keys-and-order-of-creation/108236/3 "2007-06-30T20:48:55Z")

</div>

Hi Lionel,

On Jun 30, 11:33 am, Lionel B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > account\_id  
> > One approach is I can allow owner\_id to be null and when creating the  
> > account I can create the account, create the user with the account\_id,  
> > and then set the owner\_id on the account. Then check that all went  
> > well. This whole dance happens in the Ruby of my Account model. This  
> > doesn’t seem like fully safe approach. Is there something better?
> 
> > I’m using PostgreSQL but am hoping there is a database agnostic solution.
> 
> This has nothing to do with RoR…  
> Just drop one of the key. Only one is needed to link your objects,  
> trying to have both refer to the other is duplicating information.

I don’t think this is duplicate information because an account can  
have more than one user but only one user is the owner user.

> For RoR specifics on one-to-one relationships, look at the documentation  
> of has\_one and belongs\_to.

I think this is a one to many and a one to one.

Any other suggestions?

Thanks  
Peter

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [June 30, 2007, 9:17pm UTC](https://www.ruby-forum.com/t/circular-foreign-keys-and-order-of-creation/108236/4 "2007-06-30T21:17:57Z")

</div>

> I don’t think this is duplicate information because an account can  
> have more than one user but only one user is the owner user.

Sorry, didn’t get it.

You have a choice : either the account can exist without an owner or a  
user can exist without an account (or both are independent). If you must  
ensure that two of them are linked you’ll have to use transactions (in  
fact I had this very same problem in another context and had to use  
transactions to make sure that both objects where created linked with  
one another).

Lionel

---

<div class="post-metadata">

**Author:** ![dracos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/ea666f/32.png) [@dracos](https://www.ruby-forum.com/u/dracos)\
**Post date:** [July 2, 2007, 11:40am UTC](https://www.ruby-forum.com/t/circular-foreign-keys-and-order-of-creation/108236/5 "2007-07-02T11:40:22Z")

</div>

Hi,  
From a relational point of view, is an account has a mandatory user, and  
a  
user has a mandatory account, a “user” IS an “account”

I would maybe model an User table, with a self-relationship for sharing  
account between users, an a flag telling if it is an account or not.  
But it depends what others attributes you got in your different table.  
If you want to retain 2 tables, you should drop one of the “not null”  
constraint (or use deferrable constraints if postgres support that)

2007/6/30, Peter M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):
