Re: How to deal with problem about ForeignKey

2009-04-29 Thread Phil Mocek

On Wed, Apr 29, 2009 at 03:06:37PM +0800, Vincent wrote:
> The situation is, i have model A and model B, B has a foreignkey
> point to A,.
> 
> now i find that i wanna delete records of A without deleting
> records of B which pointing to A.

You're trying to do something that a relational database
management system is specifically designed to prevent you from
doing.  This would be a violation of referential integrity.

If you delete the foreign key, then the records of B which refer
to it would cease to be valid, destroying the integrity of your
database, so the DBMS shouldn't allow you to do so.  Typically,
you'd configure your DBMS to disallow the deletion from A, cascade
the deletion to B, or update the record in B to NULL or some
default foreign key.

See: 

This information is not specific to Django, but applies to all
relational databases.  Maybe someone else can tell you how to
select one of the aforementioned options using Django's ORM.

-- 
Phil Mocek

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: How to deal with problem about ForeignKey

2009-04-29 Thread Malcolm Tredinnick

On Wed, 2009-04-29 at 01:36 -0700, Margie wrote:
[...]
> Would love to hear a summary of where this might be going from any
> developers!  I realize it is a tough problem - not complaining here.
> Just trying to get a grip on what do do in my own app and whether
> there are any changes coming and if so, what they would be and what
> the timeframe might be.

Here is where it is going: At some point, we would like to have a
manageable solution here. At no point are we interested in putting in
something half-baked. We already have something half-baked and changing
to something else half-baked isn't going to help things. As soon as a
great solution is implemented, we will include it. Until then, people
have to make do and hopefully people for whom this is an obstacle to
their work will be sufficiently motivated to come up with aforementioned
great solution.

When all that will happen is no more specific than "when it's done",
simply because we can't be more specific than that.

Regards,
Malcolm



--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: How to deal with problem about ForeignKey

2009-04-29 Thread Margie

I had a post on this recently that you would probably find relevant,
see:

http://groups.google.com/group/django-users/browse_frm/thread/582f21486c1073eb/8af00e9c0577a5e6?lnk=gst=margie#8af00e9c0577a5e6

Or search for "deletion of distant related objects" in this group.

I'm still somewhat confused on how to deal with this at a high level,
in an environment where one has apps that are written separately from
each other.  For example, suppose developer A writes an app called
"BookStore" that contains a Book model.  Now suppose developer B
integrates BookStore into his/her own "Book Club", and has a Reader
model that references the Book model through a foreign key.  It seems
to me that if the BookStore app provides a ui for deleting books, that
this has the very negative consequence of deleting Readers, which
probably isn't what the Book Club developer wants.

In my other post Malcolm gave me some pointers for finding all of the
fields that reference a particular key.  IE, if the the BookStore app
wants to provide a ui for deleting Books, it could find all fields
that reference Books through a foreign key, and set those foreign keys
to null prior to deleting the Books.  This would work, however it
makes certain very nice parts of Django unusable -  namely the
formsets.  Suppose I have a formet of books and want to use the Delete
funcionality of the formset.  It's not clear to me if/how I can set
all fields that reference Books through a foreign key to null, prior
to the automatic delete getting called.  Perhaps I just have to
sacrifice the automatic delete and have my own delete checkbox in the
formset forms, and when I see that I set all the foreign keys to null.

I am just now going into production with my app, and trying to figure
out what model to present to my users.  Currently I have a bunch of
formsets but have told my users "Don't use Delete!" (Of course I can
remove the Delete checkbox - and I will).  But I'm curious what the
plan going forward is for all of this.  I have seen disucssion on the
developers group about it but it is not clear to me if there is a plan
or if it has been tabled or if it is still in discussion.

Would love to hear a summary of where this might be going from any
developers!  I realize it is a tough problem - not complaining here.
Just trying to get a grip on what do do in my own app and whether
there are any changes coming and if so, what they would be and what
the timeframe might be.

Margie



On Apr 28, 10:57 pm, Vincent  wrote:
> Hi,
> I got a problem like this:
>
> class A(models.Model):
>     id = models.AutoField(primary_key=True)
>     field1 = models.TextField()
>
> class B(models.Model):
>     a = models.ForeignKey(A)
>     field1 = models.TextField()
>
> then there are some records of A/B,
> how can i delete records of A and do not delete records of B which have
> foreignkey on A
>
> Maybe i could change 'a = models.ForeignKey(A)'  to ' a =
> models.IntegerField("A") '.
>
> Is it a suitable way for this problem?
>
> Thanks for your suggestion.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: How to deal with problem about ForeignKey

2009-04-29 Thread James Bennett

On Wed, Apr 29, 2009 at 3:59 AM, Daniel Roseman
 wrote:
> You could try defining the foreign key with null=True.
>    a = models.ForeignKey(A, null=True)

Not really, since there's no support whatsoever for ON DELETE triggers
in the ORM.


-- 
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: How to deal with problem about ForeignKey

2009-04-29 Thread Daniel Roseman

On Apr 29, 8:06 am, Vincent  wrote:
> No, i did not mean this.
> The situation is, i have model A and model B, B has a foreignkey point to
> A,.
>
> now i find that i wanna delete records of A without deleting records of B
> which pointing to A.
> i think i could reset the B's foreignkey field to integerfield.
>
> But i consider it is not the best answer, because there are a lot of similar
> problems, and some of which should be delete, the others should not be
> delete.
>
> is there any suggestion.
>

You could try defining the foreign key with null=True.
a = models.ForeignKey(A, null=True)
--
DR.
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: How to deal with problem about ForeignKey

2009-04-29 Thread Vincent
No, i did not mean this.
The situation is, i have model A and model B, B has a foreignkey point to
A,.

now i find that i wanna delete records of A without deleting records of B
which pointing to A.
i think i could reset the B's foreignkey field to integerfield.

But i consider it is not the best answer, because there are a lot of similar
problems, and some of which should be delete, the others should not be
delete.

is there any suggestion.

2009/4/29 Marcelo Ramos 

>
> On Wed, Apr 29, 2009 at 2:57 AM, Vincent  wrote:
> > Hi,
> > I got a problem like this:
> > class A(models.Model):
> > id = models.AutoField(primary_key=True)
> > field1 = models.TextField()
> > class B(models.Model):
> > a = models.ForeignKey(A)
> > field1 = models.TextField()
> > then there are some records of A/B,
> > how can i delete records of A and do not delete records of B which have
> > foreignkey on A
>
> Did you mean how to delete records of A for which there are no records
> in B pointing to them through the foreign key "a", right?
>
> --
> Marcelo Ramos
> Django/Python developer
>
> >
>

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



Re: How to deal with problem about ForeignKey

2009-04-29 Thread Marcelo Ramos

On Wed, Apr 29, 2009 at 2:57 AM, Vincent  wrote:
> Hi,
> I got a problem like this:
> class A(models.Model):
>     id = models.AutoField(primary_key=True)
>     field1 = models.TextField()
> class B(models.Model):
>     a = models.ForeignKey(A)
>     field1 = models.TextField()
> then there are some records of A/B,
> how can i delete records of A and do not delete records of B which have
> foreignkey on A

Did you mean how to delete records of A for which there are no records
in B pointing to them through the foreign key "a", right?

-- 
Marcelo Ramos
Django/Python developer

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---



How to deal with problem about ForeignKey

2009-04-28 Thread Vincent
Hi,
I got a problem like this:

class A(models.Model):
id = models.AutoField(primary_key=True)
field1 = models.TextField()

class B(models.Model):
a = models.ForeignKey(A)
field1 = models.TextField()

then there are some records of A/B,
how can i delete records of A and do not delete records of B which have
foreignkey on A

Maybe i could change 'a = models.ForeignKey(A)'  to ' a =
models.IntegerField("A") '.

Is it a suitable way for this problem?

Thanks for your suggestion.

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~--~~~~--~~--~--~---