# ActiveRecord: FK constraints problem

**URL:** <https://www.ruby-forum.com/t/activerecord-fk-constraints-problem/61835>\
**Category:** Rails\
**Created:** [May 28, 2006, 4:36pm UTC](https://www.ruby-forum.com/t/activerecord-fk-constraints-problem/61835 "2006-05-28T16:36:31Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![zven](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/77aa72/32.png) [@zven](https://www.ruby-forum.com/u/zven)\
**Post date:** [May 28, 2006, 4:36pm UTC](https://www.ruby-forum.com/t/activerecord-fk-constraints-problem/61835/1 "2006-05-28T16:36:31Z")

</div>

Hi all.

I have has\_and\_belongs\_to\_many association between models: Section and  
Content.

class Section \< ActiveRecord::Base  
acts\_as\_tree  
has\_and\_belongs\_to\_many :contents  
belongs\_to :default\_content, :class\_name =\> ‘Content’, :foreign\_key =\>  
‘default\_content\_id’, :dependent =\> :nullify  
end

class Content \< ActiveRecord::Base  
belongs\_to :section  
end

When I trying to remove some Content which associated with Section I  
have an error related to foreigh key violation:

“PGError: ERROR: update or delete on “contents” violates foreign key  
constraint “fk\_contents\_contents\_sections” on “contents\_sections”  
DETAIL: Key (id)=(7) is still referenced from table  
“contents\_sections”.  
: DELETE FROM contents  
WHERE id = 7”

And when I trying to remove Content which belongs to “default\_content”  
of some Section  
I have an error:

PGError: ERROR: update or delete on “contents” violates foreign key  
constraint “fk\_sections\_contents” on “sections”  
DETAIL: Key (id)=(8) is still referenced from table “sections”.  
: DELETE FROM contents  
WHERE id = 8

I simply don’t understand why rails can’t determine that relation exists  
and can’t  
remove records from join-table transparently…

Please tell me what I must define to got happiness?  
I think I should create another model like SectionContent and make  
relation like ‘has\_many :contents, :through =\> :section\_contents’… Is  
there another way?

Thanks in advance.

CREATE TABLE sections (  
id serial NOT NULL,  
parent\_id integer,  
default\_content\_id integer  
);

CREATE TABLE contents (  
id serial NOT NULL,  
alias character varying(32) NOT NULL,  
name character varying(64) NOT NULL,  
title character varying(64) NOT NULL,  
keywords character varying(256),  
description character varying(512),  
contents character varying(2048),  
last\_modified timestamp without time zone  
);

CREATE TABLE contents\_sections (  
section\_id integer NOT NULL,  
content\_id integer NOT NULL  
);

ALTER TABLE ONLY contents\_sections  
ADD CONSTRAINT fk\_contents\_contents\_sections FOREIGN KEY  
(content\_id) REFERENCES contents(id);

ALTER TABLE ONLY contents\_sections  
ADD CONSTRAINT fk\_sections\_contents\_sections FOREIGN KEY  
(section\_id) REFERENCES sections(id);

ALTER TABLE ONLY sections  
ADD CONSTRAINT fk\_sections\_contents FOREIGN KEY (default\_content\_id)  
REFERENCES contents(id);

---

<div class="post-metadata">

**Author:** ![zven](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/77aa72/32.png) [@zven](https://www.ruby-forum.com/u/zven)\
**Post date:** [May 28, 2006, 4:51pm UTC](https://www.ruby-forum.com/t/activerecord-fk-constraints-problem/61835/2 "2006-05-28T16:51:56Z")

</div>

On Sun, 2006-05-28 at 20:35 -0500, zven wrote:

> “PGError: ERROR: update or delete on “contents” violates foreign key  
> constraint “fk\_contents\_contents\_sections” on “contents\_sections”  
> DETAIL: Key (id)=(7) is still referenced from table “contents\_sections”.  
> : DELETE FROM contents  
> WHERE id = 7”

“has\_and\_belongs\_to\_many :sections” on Content resolves first problem

But the second still confusing me…
