0

I am trying to filter my database by the availablity of users. The availability is set up using a time (either morning, afternoon, evening, or afterhours) and a day (example: 20)

The models in question are as follows:

class Profile(LocationModel):
    # Link with User Account, contains name and creation as well
    user = models.OneToOneField(User)
    avatar = models.BinaryField(null=True)
    # Personal Info
    birthday = models.DateField('birthdate', null=True)
    gender = models.CharField('gender',max_length=15)
    # Conections to other Databases


    def __str__(self): 
        return self.user.username    


User.profile = property(lambda u: Profile.objects.get_or_create(user=u)[0])

class Availability(models.Model):
     time = models.CharField(max_length = 11)
     day = models.CharField( max_length = 2)
     profile = models.ManyToManyField(Profile)
     def __str__(self):
        return "%s %s" % (self.time , self.day)

To test I open the shell run a script to populate the db with time/day info and then do the following:

  a = Availability.objects.all()[0]   # stores a time and days
    profile = Profile.objects.get(id=2) # stores the user
    a.profile.add(profile) 
enter code here
    Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py", line 924, in add
    self._add_items(self.source_field_name, self.target_field_name, *objs)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/fields/related.py", line 1028, in _add_items
    new_ids = new_ids - set(vals)
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 141, in __iter__
    self._fetch_all()
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 966, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 1202, in iterator
    for row in self.query.get_compiler(self.db).results_iter():
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 700, in results_iter
    for rows in self.execute_sql(MULTI):
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/sql/compiler.py", line 786, in execute_sql
    cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 81, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
ProgrammingError: relation "backend_service_availability_profile" does not exist
LINE 1: ...d_service_availability_profile"."profile_id" FROM "backend_s...

I am fairly new to django so I may be making a simple mistake, but for the life of me I can't figure it out. I have done migrate, makemigrations, and syncdb since making changes and it still gives the same error. I am using Django Version 1.7 Thanks!

BigNub
  • 1
  • 1
  • Looks like there are a few issues here... 1) `X = Profiles.objects.all()[0]` and `y = Profiles.objects.all()[1]` should use `Profile` instead of `Profiles`. 2) `Profile.objects.filter(availability__time = "morning")` doesn't looks right... try `Availability.objects.filter(time="morning", profile=profile)` (you'll need to define `profile` here, so just capture the first item in the list of profiles and that should do the trick). Let me know what happens. – Hybrid Mar 22 '15 at 02:26
  • I apologize... I have been using Profile not Profiles (changed the name a bit ago and it still slips in on accident sometimes.) as for number 2 I went ahead and set profile = Profile.objects.all()[0] then did the Availability.objects.filter(time = "morning", profile = profile) and got this: >ProgrammingError: relation "backend_service_availability_profile" does not exist LINE 1: ...y" FROM "backend_service_availability" INNER JOIN "backend_s... – BigNub Mar 22 '15 at 03:12
  • Another issue: `x.availability = a` and `y.availability = b` don't make sense - try `a.profile.add(profile)` and `b.profile.add(profile)` to add to a ManyToMany field (you can use the same `profile` variable as before). If this doesn't work, then as the error hints, it may just be that the relation doesn't exist yet on your DB level, in which case you would need to migrate (it's good that we are fixing the other issues as well, though). Let me know what happens. – Hybrid Mar 22 '15 at 03:37
  • When using a.profile.add(profile) I get this error: AttributeError: 'QuerySet' object has no attribute 'profile' – BigNub Mar 22 '15 at 03:43
  • Looks like the `profile` variable is of type `QuerySet` (which you can't run the `.add()` method on) - try making `profile = Profile.objects.get(id=1)` (or replace `1` with a valid profile ID). This should be the last step in knowing if this is a DB syncing issue/migration misstep. – Hybrid Mar 22 '15 at 03:51
  • `profile = Profile.objects.get(id = 2) a.profile.add(profile)` got the same error. AttributeError: 'QuerySet' object has no attribute 'profile' – BigNub Mar 22 '15 at 03:55
  • I see what is happening - `a = Availability.objects.all()[2:5]` and `b = Availability.objects.all()[3:5]` is making `a` and `b` of type `QuerySet` (since they hold multiple Availability objects). Make both of the numbers `[0]` so that it gets the first item in the list (I will explain later how to add to multiple items in a QuerySet). – Hybrid Mar 22 '15 at 04:03
  • Code is changed in the main question. I have the same relation does not exist error as before. – BigNub Mar 22 '15 at 04:15
  • Alright, so as I said before, it looks like a migration issue here. At least we fixed the other stuff, which would have given you errors after you migrated properly. So, the next step would be to get Django to create the `backend_service_availability_profile` table. http://stackoverflow.com/questions/24912173/django-1-7-makemigrations-not-detecting-changes - take a look at answer #1 and answer #3. – Hybrid Mar 22 '15 at 04:41
  • I have followed both suggestions in 1 and 3. `python.manage.py makemigrations backend_service python manage.py migrate backend_service' followed by the code above. I get the same error about no relation exists. To follow 3 I went into my Migrations folder and deleted everything but the __init__.py file. If this isn't the correct way to do it that might be the problem? – BigNub Mar 22 '15 at 05:29
  • Possibly related: http://stackoverflow.com/questions/27792465/django-manytomanyfield-programmingerror-relation-foo-bar-does-not-exist-rec. Looks like a Django issue to me? – SaeX Mar 22 '15 at 14:21
  • `I have done migrate, makemigrations`: are you sure you also tried `makemigrations` **first** and then `migrate`? – SaeX Mar 22 '15 at 14:24
  • Yes, after deleting the information in the DB I did makemigrations first and got this: `Migrations for backend_service: 0001_initial.py. list of models, list of fields` Then I ran migrate which gave this: `Operations to perfom: apply all migrations: backend_service Running migrations: no migrations to apply` – BigNub Mar 22 '15 at 15:02

0 Answers0