Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

on update cascade #20

Closed
menelike opened this issue Apr 23, 2013 · 2 comments
Closed

on update cascade #20

menelike opened this issue Apr 23, 2013 · 2 comments

Comments

@menelike
Copy link

Hello,

currently an update on the parent table doesn't update the children e.g. in mysql it results in "Cannot delete or update a parent row". Deletes work (warning in the admin menu doesn't include the children). Setting ON UPDATE CASCADE on the foreign key in mysql does the job, but breaks the concept of django with emulating the cascades.

Besides: Great work simone, you've got the only working compound PK solution working.

from compositekey import db
from django.db import models

class Blog(models.Model):
id = db.MultiFieldPK("title", "author")
title = models.CharField(max_length=100)
author = models.CharField(max_length=100)

class Meta:
    app_label = 'foo'

class Post(models.Model):
post = models.CharField(max_length=100)
blog = models.ForeignKey(Blog, to_field="id",
fields_ext={
"author": {"db_column" :"author"},
"title" : {"db_column" :"title"},
})
class Meta:
app_label = 'foo'

CREATE TABLE IF NOT EXISTS solar_post (
id int(11) NOT NULL AUTO_INCREMENT,
title varchar(100) NOT NULL,
author varchar(100) NOT NULL,
post varchar(100) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY bar (title,author),
KEY solar_post_841a7e28 (title),
KEY solar_post_a5d5a658 (author)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

OLD:
ALTER TABLE solar_post ADD CONSTRAINT solar_post_ihgdbfk_1 FOREIGN KEY (title, author) REFERENCES solar_blog (title, author)

NEW:
ALTER TABLE solar_post ADD CONSTRAINT solar_post_ihgdbfk_1 FOREIGN KEY (title, author) REFERENCES solar_blog (title, author) ON UPDATE CASCADE

@simone
Copy link
Owner

simone commented Apr 24, 2013

Thanks for the feedback.

On Tue, Apr 23, 2013 at 10:51 PM, menelike notifications@github.com wrote:

Hello,

currently an update on the parent table doesn't update the children e.g.
in mysql it results in "Cannot delete or update a parent row". Deletes work
(warning in the admin menu doesn't include the children). Setting ON UPDATE
CASCADE on the foreign key in mysql does the job, but breaks the concept of
django with emulating the cascades.

Besides: Great work simone, you've got the only working compound PK
solution working.

from compositekey import db
from django.db import models

class Blog(models.Model):
id = db.MultiFieldPK("title", "author")
title = models.CharField(max_length=100)
author = models.CharField(max_length=100)

class Meta:
    app_label = 'foo'

class Post(models.Model):
post = models.CharField(max_length=100)
blog = models.ForeignKey(Blog, to_field="id",
fields_ext={
"author": {"db_column" :"author"},
"title" : {"db_column" :"title"},
})
class Meta:
app_label = 'foo'

CREATE TABLE IF NOT EXISTS solar_post (
id int(11) NOT NULL AUTO_INCREMENT,
title varchar(100) NOT NULL,
author varchar(100) NOT NULL,
post varchar(100) NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY bar (title,author),
KEY solar_post_841a7e28 (title),
KEY solar_post_a5d5a658 (author)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;

#OLD:
#ALTER TABLE solar_post

ADD CONSTRAINT solar_post_ihgdbfk_1 FOREIGN KEY (title, author) REFERENCES solar_blog (title, author)

#NEW
ALTER TABLE solar_post
ADD CONSTRAINT solar_post_ihgdbfk_1 FOREIGN KEY (title, author) REFERENCES solar_blog (title, author) ON UPDATE CASCADE


Reply to this email directly or view it on GitHubhttps://github.com//issues/20
.

@menelike
Copy link
Author

Hi Simone,

the reason I deleted the issue is that it isn't actually an issue. Django primary keys should be immutable, even on normal foreign key relations there is no ON UPDATE CASCADE feature in django itself. This results in that primary compund keys should be immutable too. In my case the primary keys are mutable, the CASCADE ON UPDATE is a database driven solution, but it's always the best solution to stay with immutable primary key(s) anyway.

Keep up the good work!

Regards

@simone simone closed this as completed May 2, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants