Thursday, November 3, 2011

Re: Relation not found error while dealing with foreign keys and forms

K, so that worked...

This is what I got:
In [5]: i = Staff.objects.using('gold').get(username='tsamuel')

In [6]: i.nics_group.n_group_name
Out[6]: u'Systems and Operations'

Looks like the foreign key is working fine from the db's perspective.
This is how I'm getting the form:
form = StaffForm(instance = Staff.objects.using('gold').get(username
=current_staff))
where current_staff = 'tsamuel'
and then if I do a print form, I get a relation n_nics_groups does not
exist error.

Once again, thank you so much for your help, I really appreciate it!

Tabitha

On Nov 3, 1:26 pm, Furbee <Furbeena...@gmail.com> wrote:
> I may have lead you astray. I set up the same models on my end and get the
> same query; it didn't show the join of the foreign key in the QuerySet of
> Staff, but when I did a get() on username='tsamuel' I got a Staff object.
> Then I was able to get the group they belonged to. This was successful for
> me:
>
> Here's my data:
>
> table: n_nics_groups
> n_group_number; n_group_name
> 1; "TestGroup1"
> 2; "TestGroup2"
> 3; "TestGroup3"
>
> table: n_test_staff
> "tfigura";"";"";"";"";"";"";"";"";2;"";"''";"";"";"";1;1;1;;"''";"''";"''";"''";"";FALSE;TRUE
> "tsamuel";"";"";"";"";"";"";"";"";1;"";"''";"";"";"";1;1;1;;"''";"''";"''";"''";"''";FALSE;TRUE
>
> 'tfigura' is in group with n_group_number 2, tsamuel is in group with
> n_group_number 1.
>
> python manage.py shell:
>
> >>> from project.models import NICSGroupType, Staff
> >>> i = Staff.objects.get(username='tsamuel')
> >>> i.nics_group.n_group_name
> >>> u'TestGroup1'
>
> Is this operational for your instance? If not, I would double check that
> the n_nics_groups table definitely exists in the datasource you defined as
> 'gold.' This should be defined in the DATABASES section of settings.py. It
> looks like it is the database 'public' from your example, but double check
> that the 'gold' DATABASE points there.
>
> Otherwise, I wonder if it is somewhere where you are defining the form
> object. First, let's verify that the foreign key reference is being honored
> on your system, by running the above test, to verify that the group is
> printed, when you get() a specific record and print the
> nics_group.n_group_name.
>
> Thanks,
>
> Furbee
>
> On Wed, Nov 2, 2011 at 5:09 PM, Tabitha Samuel <tabitha.sam...@gmail.com>wrote:> Thank you so much for your reply! So I got a "Staff object has no
> > attribute 'query'" error when I did it with the get. I got the sql
> > when I tried it with the filter instead (instance =
> > Staff.objects.using('gold').filter(username='tsamuel') >>
> > str(instance.query))this is what I'm getting:
>
> > 'SELECT "n_test_staff"."username", "n_test_staff"."home_phone",
> > "n_test_staff"."cell_phone", "n_test_staff"."home_address",
> > "n_test_staff"."home_city", "n_test_staff"."home_state",
> > "n_test_staff"."home_zip", "n_test_staff"."emergency_name",
> > "n_test_staff"."emergency_phone", "n_test_staff"."nics_group",
> > "n_test_staff"."room_number", "n_test_staff"."title",
> > "n_test_staff"."supervisor", "n_test_staff"."url",
> > "n_test_staff"."im", "n_test_staff"."last_modification_time",
> > "n_test_staff"."start_date", "n_test_staff"."creation_time",
> > "n_test_staff"."termination_date", "n_test_staff"."bio",
> > "n_test_staff"."photopath", "n_test_staff"."office_phone",
> > "n_test_staff"."email", "n_test_staff"."preferred_name",
> > "n_test_staff"."deleted", "n_test_staff"."viewable" FROM
> > "n_test_staff" WHERE "n_test_staff"."username" = tsamuel '
>
> > Looks like from the query, is not looking into the n_nics_groups
> > table. Question is why?
>
> > Tabitha
>
> > On Nov 2, 6:00 pm, Furbee <Furbeena...@gmail.com> wrote:
> > > Can you try this and tell us what you see:
>
> > > Run a shell using python manage.py shell
>
> > > >>> instance = Staff.objects.using('gold').get(username='tsamuel')
> > > >>> str(instance.query)
>
> > > This will tell us whether or not the database, reference, and such are
> > > correctly translating into a query. The error means Django is sending an
> > > erroneous query to your database layer.
>
> > > Furbeenator
>
> > > On Wed, Nov 2, 2011 at 1:54 PM, Tabitha Samuel <tabitha.sam...@gmail.com
> > >wrote:
>
> > > > Hi,
>
> > > > In brief here is my problem. I have two simple tables, one has a one
> > > > to many relation with the other. The problem I run into is that when I
> > > > try to create a form instance of the child, and try to print it or
> > > > render it in a template, I run into a "relation not found" error for
> > > > the parent. Simply querying the parent works without a problem.
>
> > > > This is what my models.py looks like:
> > > > from django.db import models
>
> > > > class NICSGroupType(models.Model):
> > > >     n_group_number = models.IntegerField(primary_key = True)
> > > >    n_group_name = models.CharField(max_length = 512)
> > > >    class Meta:
> > > >        db_table = "n_nics_groups"
>
> > > > class Staff(models.Model):
> > > >    username                    = models.CharField(primary_key = True,
> > > > max_length = 50)
> > > >    home_phone                  = models.CharField(max_length = 12,
> > > > null=True)
> > > >    cell_phone                  = models.CharField(max_length = 12,
> > > > null = True)
> > > >    home_address                = models.CharField(max_length = 1024,
> > > > null = True)
> > > >    home_city                   = models.CharField(max_length = 64,
> > > > null = True)
> > > >    home_state                  = models.CharField(max_length = 32,
> > > > null = True)
> > > >    home_zip                    = models.CharField(max_length = 10,
> > > > null = True)
> > > >    emergency_name              = models.CharField(max_length =64,
> > > > null = True)
> > > >    emergency_phone             = models.CharField(max_length = 12,
> > > > null = True)
> > > >    nics_group                  = models.ForeignKey(NICSGroupType,
> > > > to_field ='n_group_number', db_column="nics_group",
> > > > null=True,blank=True)
> > > >    room_number                 = models.CharField(max_length = 32,
> > > > null = True)
> > > >    title                       = models.CharField(max_length = 64)
> > > >    supervisor                  = models.CharField(max_length = 25,
> > > > null = True, blank = True)
> > > >    url                         = models.CharField(max_length =
> > > > 256,null = True, blank = True)
> > > >    im                          = models.CharField(max_length = 32,
> > > > null = True, blank=True)
> > > >    last_modification_time      = models.IntegerField()
> > > >    start_date                  = models.IntegerField()
> > > >    creation_time               = models.IntegerField()
> > > >    termination_date            = models.IntegerField(null = True,
> > > > blank = True)
> > > >    bio                         = models.TextField()
> > > >    photopath                   = models.CharField(max_length = 5048)
> > > >    office_phone                = models.CharField(max_length=12)
> > > >    email                       = models.CharField(max_length = 256)
> > > >    preferred_name              = models.CharField(max_length = 50,
> > > > null = True, blank = True)
> > > >    deleted                     = models.BooleanField(default = False)
> > > >    viewable                    = models.BooleanField(default = True)
>
> > > >    class Meta:
> > > >        db_table = "n_test_staff"
>
> > > > The tables that it corresponds to looks like this:
>
> > > >  \d n_test_staff
> > > >                 Table "public.n_test_staff"
> > > >         Column         |          Type          | Modifiers
> > > > ------------------------+------------------------+-----------
> > > >  username               | character varying(25)  |
> > > >  home_phone             | character varying(12)  |
> > > >  cell_phone             | character varying(12)  |
> > > >  home_address           | character varying(256) |
> > > >  home_city              | character varying(64)  |
> > > >  home_state             | character varying(32)  |
> > > >  home_zip               | character varying(10)  |
> > > >  emergency_name         | character varying(64)  |
> > > >  emergency_phone        | character varying(12)  |
> > > >  nics_group             | integer                |
> > > >  room_number            | character varying(32)  |
> > > >  title                  | character varying(64)  |
> > > >  supervisor             | character varying(25)  |
> > > >  url                    | character varying(256) |
> > > >  im                     | character varying(32)  |
> > > >  last_modification_time | integer                |
> > > >  start_date             | integer                |
> > > >  creation_time          | integer                |
> > > >  termination_date       | integer                |
> > > >  bio                    | text                   |
> > > >  photopath              | text                   |
> > > >  office_phone           | character varying(12)  |
> > > >  email                  | text                   |
> > > >  preferred_name         | character varying(50)  |
> > > >  deleted                | boolean                |
> > > >  viewable               | boolean                |
> > > > Foreign-key constraints:
> > > >    "nics_group_fkey" FOREIGN KEY (nics_group) REFERENCES
> > > > n_nics_groups(n_group_number) DEFERRABLE INITIALLY DEFERRED
>
> > > > n_nics_groups
> > > >            Table "public.n_nics_groups"
> > > >     Column     |          Type          | Modifiers
> > > > ----------------+------------------------+-----------
> > > >  n_group_number | integer                | not null
> > > >  n_group_name   | character varying(256) |
> > > >  n_group_lead   | character varying(25)  |
> > > > Indexes:
> > > >    "n_nics_groups_pkey" PRIMARY KEY, btree (n_group_number)
>
> > > > So when I do a
> > > >  form = StaffForm(instance = Staff.objects.using('gold').get(username
> > > > ='tsamuel'))
> > > >    print form
>
> > > > the print form throws the error:
> > > >        relation "n_nics_groups" does not exist
>
> > > > I know the model can "see" the relation on other occassions because
> > > > statements like
> > > > groups = NICSGroupType.objects.using('gold').all()  work without a
> > > > problem. Wondering why the foreign key causes this problem. I hope you
> > > > can help me with this issue as I haven't been able to find much help
> > > > anywhere else on the web (and I've been looking into this for about a
> > > > week now!) Please let me know if you need any more information.
> > > > Thanks!!
>
> > > > Tabitha Samuel
> > > > <tabitha.sam...@gmail.com>
>
> > > > --
> > > > 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+unsubscribe@googlegroups.com.
> > > > For more options, visit this group at
> > > >http://groups.google.com/group/django-users?hl=en.
>
> > --
> > 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+unsubscribe@googlegroups.com.
> > For more options, visit this group at
> >http://groups.google.com/group/django-users?hl=en.

--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment