Friday, November 4, 2011

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

That is very strange... I recreated this on my development system and it worked fine. I looked for Django bugs, but haven't found any related to this issue. To be clear, the table n_nics_groups definitely exists in the same database as n_test_staff, right? It does in mine because I used python manage.py syncdb to create them from the models. If so, maybe we should try to create a raw() query to see if the system has a bug creating the query. This should be similar:

form = StaffForm(instance = Staff.objects.raw("SELECT s.*, g.n_group_name FROM n_test_staff s LEFT JOIN n_nics_groups g ON(g.n_group_number = s.nics_group) WHERE s.username = 'tsamuel'))

If that throws an error, we may have a problem in the DB layer.

Thanks,

Furbee

On Fri, Nov 4, 2011 at 10:29 AM, Tabitha Samuel <tabitha.samuel@gmail.com> wrote:
The error that I'm getting is on the second form instantiation, that
is on the line:

form = StaffForm(instance = Staff.objects.using('gold').get(username =
current_staff)
where current_staff='tsamuel' for instance

This is the traceback of the error that I get when I do a print form
right after getting the form:
Environment:


Request Method: GET
Request URL: http://watermelon.nics.utk.edu:8004/staff/staffinfo

Django Version: 1.3.1
Python Version: 2.6.2
Installed Applications:
['django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.sites',
 'gibbs.quartermaster',
 'gibbs.userportal',
 'gibbs.reports',
 'gibbs.events',
 'gibbs.job_stats',
 'gibbs.simulator',
 'gibbs.staff']
Installed Middleware:
('django.contrib.csrf.middleware.CsrfViewMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.contrib.csrf.middleware.CsrfResponseMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware')


Traceback:
File "/nics/a/applications/gibbs/python/site-packages_django/django/
core/handlers/base.py" in get_response
 111.                         response = callback(request,
*callback_args, **callback_kwargs)
File "/nics/a/applications/gibbs/python/site-packages_django/django/
contrib/auth/decorators.py" in _wrapped_view
 23.                 return view_func(request, *args, **kwargs)
File "/nics/a/home/tsamuel/tssandbox/gibbs/utils/decorators.py" in
decorate
 11.         return view_func(request, *args, **kws)
File "/nics/a/home/tsamuel/tssandbox/gibbs/../gibbs/staff/views.py" in
staff_info
 159.     print form
File "/nics/a/applications/gibbs/python/site-packages_django/django/
utils/encoding.py" in __str__
 27.         return self.__unicode__().encode('utf-8')
File "/nics/a/applications/gibbs/python/site-packages_django/django/
forms/forms.py" in __unicode__
 95.         return self.as_table()
File "/nics/a/applications/gibbs/python/site-packages_django/django/
forms/forms.py" in as_table
 217.             errors_on_separate_row = False)
File "/nics/a/applications/gibbs/python/site-packages_django/django/
forms/forms.py" in _html_output
 180.                     'field': unicode(bf),
File "/nics/a/applications/gibbs/python/site-packages_django/django/
forms/forms.py" in __unicode__
 408.         return self.as_widget()
File "/nics/a/applications/gibbs/python/site-packages_django/django/
forms/forms.py" in as_widget
 439.         return widget.render(name, self.value(), attrs=attrs)
File "/nics/a/applications/gibbs/python/site-packages_django/django/
forms/widgets.py" in render
 516.         options = self.render_options(choices, [value])
File "/nics/a/applications/gibbs/python/site-packages_django/django/
forms/widgets.py" in render_options
 533.         for option_value, option_label in chain(self.choices,
choices):
File "/nics/a/applications/gibbs/python/site-packages_django/django/
forms/models.py" in __iter__
 882.             for obj in self.queryset.all():
File "/nics/a/applications/gibbs/python/site-packages_django/django/db/
models/query.py" in _result_iter
 107.                 self._fill_cache()
File "/nics/a/applications/gibbs/python/site-packages_django/django/db/
models/query.py" in _fill_cache
 772.
self._result_cache.append(self._iter.next())
File "/nics/a/applications/gibbs/python/site-packages_django/django/db/
models/query.py" in iterator
 273.         for row in compiler.results_iter():
File "/nics/a/applications/gibbs/python/site-packages_django/django/db/
models/sql/compiler.py" in results_iter
 680.         for rows in self.execute_sql(MULTI):
File "/nics/a/applications/gibbs/python/site-packages_django/django/db/
models/sql/compiler.py" in execute_sql
 735.         cursor.execute(sql, params)
File "/nics/a/applications/gibbs/python/site-packages_django/django/db/
backends/util.py" in execute
 34.             return self.cursor.execute(sql, params)
File "/nics/a/applications/gibbs/python/site-packages_django/django/db/
backends/postgresql_psycopg2/base.py" in execute
 44.             return self.cursor.execute(query, args)

Exception Type: DatabaseError at /staff/staffinfo
Exception Value: relation "n_nics_groups" does not exist


Tabitha


On Nov 4, 11:31 am, Furbee <Furbeena...@gmail.com> wrote:
> Oh no, that would not be a good thing to share! :-) That's a bummer that
> the upgrade broke things.
>
> So, the error is being raised on this line?
> staff_form = StaffForm(request.POST, instance=staffinstance).using('gold')
>
> Furbee
>
> On Fri, Nov 4, 2011 at 8:02 AM, Tabitha Samuel <tabitha.sam...@gmail.com>wrote:
>
>
>
>
>
>
>
> > So this is how I'm creating the staff form:
>
> > def staff_info(request, *args, **kws):
> >    class StaffForm(forms.ModelForm):
> >        class Meta:
> >          model= Staff
> >          ....
> >          ....
> >          ....
> >  if request.method == 'POST' and request.POST['event'] ==
> > 'choosestaff':
> > current_staff =
> > request.POST.get('selectstaff')
> > elif request.method == 'POST' and request.POST['event'] ==
> > 'editstaff':
> > #print
> > request
> > current_staff =
> > request.POST.get('username')
> > errors =
> > validate_staff(request)
> > if errors is not None and len(errors) ==
> > 0:
> > print
> > request.POST
> > staffinstance = Staff.objects.using('gold').get(username =
> > current_staff)
> > #new_form =
> > StaffForm(instance=staffinstance)
> > #print
> > new_form
> > staff_form = StaffForm(request.POST,
> > instance=staffinstance).using('gold')
> > staff_form.last_modification_time =
> > datetime.now()
> > staff_form.supervisor =
> > staffinstance.supervisor
> > staff_form.start_date =
> > staffinstance.start_date
> >            staff_form.creation_time =
> > staffinstance.creation_time
> >            staff_form.termination_date =
> > staffinstance.termination_date
> >           staff_form.using('gold').save()
>
> > else:
> >        current_staff =
> > request.user.username
> >    tg_info,staff, nics_info =
> > get_tg_info(current_staff)
>
> >    groups =
> > NICSGroupType.objects.using('gold').all()
> >     form = StaffForm(instance =
> > Staff.objects.using('gold').select_related().get(username =
> > current_staff))
> >    print
> > form
> >    return render_to_response('staff/staffinfo.html',{'form':form,
> > 'tginfo':tg_info, 'nicsinfo': nics_info, 'staff':staff,
> > 'is_admin':is_admin, 'errors':errors},context_instance =
> > RequestContext(request))
>
> > Funny thing is that this worked perfectly in version 1.1a of django.
> > All trouble started when I switched to 1.3. I was using extDbManager
> > to create the gold connection in the 1.1 version since 'gold' is a non-
> > default db (used only for this model) and 1.1 only had in built
> > support for a single db.
>
> > And yes, I do have a page for the view. Problem is that it is passcode
> > protected and I don't think I'm at liberty to give you access to it. :
> > (
>
> > Tabitha
> > On Nov 4, 10:44 am, Mark Furbee <markfur...@gmail.com> wrote:
> > > Good Morning Tabitha.
>
> > > Actually, do you have a model for StaffForm? That is the object being
> > > instantiated and then causing the error. If there is no nics group
> > > reference field in the StaffForm object, it may raise this error. Also,
> > do
> > > you have this running in a view/template? Do you have a real page that
> > > you've tried this on, or have you just tried it in the shell?
>
> > > Thanks,
>
> > > Furbee
>
> > > On Fri, Nov 4, 2011 at 7:02 AM, Tabitha Samuel <tabitha.sam...@gmail.com
> > >wrote:
>
> > > > Here is staff/models.py
>
> > > > 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)
> > > >     def __str__(self):
> > > >        return self.n_group_name
> > > >     class Meta:
> > > >        db_table = "n_nics_groups"
>
> > > > class StaffpageAdmin(models.Model):
> > > >    n_id                        = models.IntegerField(primary_key =
> > > > True)
> > > >    n_username                  = models.CharField(max_length = 50)
> > > >    class Meta:
> > > >        db_table = 'n_staffpage_admin'
>
> > > > 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"
>
> > > > class TG_Staff(models.Model):
> > > >    g_name                      = models.CharField(primary_key = True,
> > > > max_length = 1024)
> > > >    g_modification_time         = models.IntegerField()
> > > >    g_active                    = models.CharField(max_length = 5)
> > > >    g_common_name               = models.CharField(max_length = 1024)
> > > >    g_phone_number              = models.CharField(max_length = 1024)
> > > >    g_default_project           = models.CharField(max_length = 1024)
> > > >    g_office_address            = models.CharField(max_length = 1024)
> > > >    g_bz_phone                  = models.CharField(max_length = 1024)
> > > >    g_bz_phone_ext              = models.CharField(max_length = 1024)
> > > >    g_citizenship               = models.CharField(max_length = 1024)
> > > >    g_street_address            = models.CharField(max_length = 1024)
> > > >    g_street_address2           = models.CharField(max_length = 1024)
> > > >    g_city                      = models.CharField(max_length = 1024)
> > > >    g_state                     = models.CharField(max_length = 1024)
> > > >    g_zip                       = models.CharField(max_length = 1024)
> > > >    g_country                   = models.CharField(max_length = 1024)
> > > >    g_dept                      = models.CharField(max_length = 1024)
> > > >    g_tg_person_id              = models.CharField(max_length = 1024)
> > > >    g_org                       = models.CharField(max_length = 1024)
> > > >    g_position                  = models.CharField(max_length = 1024)
> > > >    g_home_phone                = models.CharField(max_length = 1024)
> > > >    g_fax                       = models.CharField(max_length = 1024)
> > > >    g_ldap_id                   = models.IntegerField()
> > > >    g_email_address             = models.CharField(max_length = 1024)
>
> > > >    class Meta:
> > > >        db_table = "g_user"
>
> > > > Is this what you needed?
>
> > > > Tabitha
>
> > > > On Nov 4, 2:00 am, Furbee <Furbeena...@gmail.com> wrote:
> > > > > Hi Tabitha,
>
> > > > > I wish I could supply a quick and simple answer to fix what's going
> > on in
> > > > > your database, but nothing obvious is standing out. Can you supply
> > your
> > > > > model for StaffForm? That seems to be where the problem lies, since
> > we
> > > > can
> > > > > get details from the model and view elsewhere.
>
> > > > > Thanks,
>
> > > > > Furbee
>
> > > > > On Thu, Nov 3, 2011 at 7:22 PM, Tabitha Samuel <
> > tabitha.sam...@gmail.com
> > > > >wrote:
>
> > > > > > 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;
>
> ...
>
> read more »

--
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