# Dependent =\> destroy without primary key?

**URL:** <https://www.ruby-forum.com/t/dependent-destroy-without-primary-key/132195>\
**Category:** Rails\
**Created:** [March 17, 2008, 8:20pm UTC](https://www.ruby-forum.com/t/dependent-destroy-without-primary-key/132195 "2008-03-17T20:20:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dino\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@dino\_d](https://www.ruby-forum.com/u/dino_d)\
**Post date:** [March 17, 2008, 8:20pm UTC](https://www.ruby-forum.com/t/dependent-destroy-without-primary-key/132195/1 "2008-03-17T20:20:31Z")

</div>

Hi-

Can this be done? When I destroy the master, I get an SQL error:

ActiveRecord::StatementInvalid (Mysql::Error: Unknown column ‘id’ in  
‘where clause’: DELETE FROM `votes`  
WHERE `id` = NULL  
):

This is because I have no ‘ID’ in this table.

So, how, in general do you automatically remove join records if there  
is no id in the join table?

Thanks,  
Dino

---

<div class="post-metadata">

**Author:** ![dino\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@dino\_d](https://www.ruby-forum.com/u/dino_d)\
**Post date:** [March 17, 2008, 8:42pm UTC](https://www.ruby-forum.com/t/dependent-destroy-without-primary-key/132195/2 "2008-03-17T20:42:30Z")

</div>

if it can’t find them to destroy them, then how does a normal find work  
if you want to show them.  
anyway, you can define those sql-fragments with the associations:

:finder\_sql - overwrite the default generated SQL statement used to  
fetch the association with a manual statement  
:delete\_sql - overwrite the default generated SQL statement used to  
remove links between the associated classes with a manual statement  
:insert\_sql - overwrite the default generated SQL statement used to add  
links between the associated classes with a manual statement

(not all of them exist for all association types)

if that’s not possible you can execute pure sql.  
but that should be only necessary, if you’re forced to use some external  
db.

---

<div class="post-metadata">

**Author:** ![dino\_d](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/b5ac83/32.png) [@dino\_d](https://www.ruby-forum.com/u/dino_d)\
**Post date:** [March 18, 2008, 2:37am UTC](https://www.ruby-forum.com/t/dependent-destroy-without-primary-key/132195/3 "2008-03-18T02:37:12Z")

</div>

dino d. wrote:

> This is because I have no ‘ID’ in this table.
> 
> So, how, in general do you automatically remove join records if there  
> is no id in the join table?

look at has\_many whatevers :dependent =\> :destroy within the rails api

hth

ilan
