1

I have two tables dim_job and dim_stream when i do python manage.py inspectdb > models.py.......... then below shown models.py(class shown) is created in django........ dim_job table has three attributes where job_name and alias combined as primary key......here alias is also a foreign key referencing dim_stream table alias. dim_stream table has two attributes where alias is primary key........... When i try to execute DimJob.objects.all() shows error.? How can data be fetched from dim_job table?django queries?

In error i see that dim_job.id doesn't exist ....I don't understand why it is showing this?Already there is combined primary key(job_name,alias) .

I am using postgres database

    class DimJob(models.Model):
          job_name = models.CharField(max_length=50)
          alias = models.ForeignKey('DimStream', db_column='alias')
          job_start_time = models.CharField(max_length=20, blank=True, null=True)

          class Meta:
              managed = False
              db_table = 'dim_job'
              unique_together = (('job_name', 'alias'),)



    class DimStream(models.Model):
          stream_name = models.CharField(max_length=50, blank=True, null=True)
          alias = models.CharField(primary_key=True, max_length=50)

          class Meta:
              managed = False
              db_table = 'dim_stream'

          def __str__(self):
              return self.stream_name




         Error:
           DimJob.objects.all()
           Traceback (most recent call last):
           File "C:\Users\vivekre\AppData\Local\Programs\Python\Python36- 
           32\lib\site-packages\django\core\management\commands\shell.py", 
           line 69, in handle
           self.run_shell(shell=options['interface'])
           File "C:\Users\vivekre\AppData\Local\Programs\Python\Python36- 
           32\lib\site-packages\django\core\management\commands\shell.py", 
           line 61, in run_shell
           raise ImportError
           ImportError

           During handling of the above exception, another exception 
           occurred:

           Traceback (most recent call last):
           File "C:\Users\vivekre\AppData\Local\Programs\Python\Python36- 
           32\lib\site-packages\django\db\backends\utils.py", line 64, in 
           execute
           return self.cursor.execute(sql, params)
           psycopg2.ProgrammingError: column dim_job.id does not exist
           LINE 1: SELECT "dim_job"."id", "dim_job"."job_name", 
           "dim_job"."alia...
           ^


              The above exception was the direct cause of the following 
              exception:

              Traceback (most recent call last):
              File "<console>", line 1, in <module>
              File "C:\Users\vivekre\AppData\Local\Programs\Python\Python36- 
              32\lib\site-packages\django\db\models\query.py", line 138, in 
               __repr__
              data = list(self[:REPR_OUTPUT_SIZE + 1])
              File "C:\Users\vivekre\AppData\Local\Programs\Python\Python36- 
              32\lib\site-packages\django\db\models\query.py", line 162, in 
             __iter__
             self._fetch_all()
              File "C:\Users\vivekre\AppData\Local\Programs\Python\Python36- 
               32\lib\site-packages\django\db\models\query.py", line 965, in 
               _fetch_all
             self._result_cache = list(self.iterator())
             File "C:\Users\vivekre\AppData\Local\Programs\Python\Python36- 
             32\lib\site-packages\django\db\models\query.py", line 238, in 
              iterator
            results = compiler.execute_sql()
             File "C:\Users\vivekre\AppData\Local\Programs\Python\Python36- 
             32\lib\site-packages\django\db\models\sql\compiler.py", line 
            829, in execute_sql
            cursor.execute(sql, params)
            File "C:\Users\vivekre\AppData\Local\Programs\Python\Python36- 
             32\lib\site-packages\django\db\backends\utils.py", line 79, in 
           execute
          return super(CursorDebugWrapper, self).execute(sql, params)
          File "C:\Users\vivekre\AppData\Local\Programs\Python\Python36- 
          32\lib\site-packages\django\db\backends\utils.py", line 64, in 
        execute
          return self.cursor.execute(sql, params)
           File "C:\Users\vivekre\AppData\Local\Programs\Python\Python36- 
         32\lib\site-packages\django\db\utils.py", line 97, in __exit__
        six.reraise(dj_exc_type, dj_exc_value, traceback)
         File "C:\Users\vivekre\AppData\Local\Programs\Python\Python36- 
       32\lib\site-packages\django\utils\six.py", line 658, in reraise
         raise value.with_traceback(tb)
         File "C:\Users\vivekre\AppData\Local\Programs\Python\Python36- 
        32\lib\site-packages\django\db\backends\utils.py", line 64, in 
        execute
          return self.cursor.execute(sql, params)
       django.db.utils.ProgrammingError: column dim_job.id does not exist
       LINE 1: SELECT "dim_job"."id", "dim_job"."job_name", 
       "dim_job"."alia...
Vivek
  • 11
  • 3
  • What error are you seeing? The error output is important. – A. L. Flanagan Jun 12 '18 at 19:07
  • I added error output – Vivek Jun 13 '18 at 04:45
  • Make sure that "ID" column exists in both of the tables. Django ORM will not work without ID. If you have a different primary key field then you have to tell django about it. If Django sees you’ve explicitly set Field.primary_key, it won’t add the automatic id column. Each model requires exactly one field to have primary_key=True (either explicitly declared or automatically added). – Hemant_Negi Jun 13 '18 at 04:48
  • DimStream.objects.all() is working it gives output event though no id is present in this table – Vivek Jun 13 '18 at 04:50
  • Your assumption that `unique_together = (('job_name', 'alias'),)` creates primary key is wrong. This will only make unique index. Django does not support composite primary keys. Same issue here: https://stackoverflow.com/questions/19999465/django-custom-primary-key – Łukasz Kamiński Jun 13 '18 at 08:00
  • i've added basic support for Composite Keys. Check my answer here - https://stackoverflow.com/a/65404017/46548 – kmmbvnr Dec 22 '20 at 06:15

0 Answers0