1

I am not sure what's wrong.
I have tried migrate/makemigrations and that doesnt help, Im working in virtualenv tho.
my models.py

    from django.db import models
import re


# Create your models here.
class Currency(models.Model):
    def __str__(self):
        return self.short
    name = models.CharField(max_length=32, null=True, blank=True)
    country = models.CharField(max_length=50, null=True, blank=True)
    short = models.CharField(max_length=10, null=True, blank=True)


class Source(models.Model):
    def __str__(self):
        return self.name + '({}...)'.format(self.url[:20])
    url = models.URLField()
    name = models.CharField(max_length=250, verbose_name='Source_name')
    timestamp = models.DateTimeField(auto_now_add=True)
    currency = models.ForeignKey(Currency)

    def save(self, *args, **kwargs):
        self.name = ''.join(re.sub('\s+', ' ', self.name).strip())
        super(Source, self).save()


class Product(models.Model):
    def __str__(self):
        return self.name
    name = models.CharField(max_length=250, blank=False)
    prices = models.ManyToManyField(to='Price', blank=True)
    date_creation = models.DateField(auto_now_add=True)
    prod_code = models.CharField(max_length=250, blank=True, null=True)

    def save(self, *args, **kwargs):
        self.name = ''.join(re.sub('\s+', ' ', self.name).strip())
        super(Product, self).save()  

My function that creates objects

def from_csv_to_db(csv_name):  # empty DB
try:
    df = pd.read_csv(csv_name, sep=';')
except FileNotFoundError:
    return ('Please input correct path to csv file or check spelling. Process finished')
for i, row in df.iterrows():
    Currency.objects.get_or_create(short=row['source__currency__short'], id=row['source__currency_id'])
    Product.objects.get_or_create(id=row['product__id'], name=row['product__name'])
    Source.objects.get_or_create(id=row['source__id'], name=row['source__name'], url = row['source__url'],
                                 currency_id=row['source__currency_id'])
    Parser.objects.get_or_create(script=row['script'], boolean=row['boolean'], product_id=row['product__id'],
                                 source_id=row['source__id'])
return 'Done'

However, django does display my models object via queryset in python manage.py shell

from Parser.models import *
Product.objects.all()  
<QuerySet [<Product: test product>, <Product: iphone>, <Product: 
Mac>, <Product: iPad>]>  

however when I run python manage.py dbshell > select * from Parser_product;
I get ERROR: relation "parser_product" does not exist

I use Docker on Debian and this is done remotely.

nexla
  • 434
  • 7
  • 20
  • 1
    Hello, have you ran `python manage.py makemigrations` and `python manage.py migrate` ? EDIT: I see you did, what is the output of those commands? – Josef Korbel Apr 11 '18 at 15:18
  • edit: the output is : for makemigrations - No changes detected migrate - No migrations to apply. I even tried specifiying what to migrate (i.e Parser model, that didnt work either) – nexla Apr 11 '18 at 15:20
  • Thats because Django prefix the table name with the appname, i think – YoLecomte Apr 11 '18 at 15:22
  • 1
    Have you added your app to `INSTALLED_APPS' in 'settings.py' ? Looks like django didnt create tables yet – Josef Korbel Apr 11 '18 at 15:22
  • Tables are there( the `python manage.py dbshell` displays them), but I get the `ERROR: relation "parser_price" does not exist` when I try to access the table. Yeap it is in INSTALLED_APPS :/ – nexla Apr 11 '18 at 15:23
  • Did you try a kind of MySql's `show tables` command to check if everything is fine? As @JosefKorbel stated, it seems there's a problem with migration. – floatingpurr Apr 11 '18 at 15:24
  • yeap I used postgres's `\dt` , was there as mentioned above, I get a weird error that relation doesn't exist when I try to query the table : – nexla Apr 11 '18 at 15:26
  • @YoLecomte could you please elaborate ? – nexla Apr 11 '18 at 15:29
  • So, you are saying you definitely have the table `Parser_product ` but you cannot query that. Right? – floatingpurr Apr 11 '18 at 15:29
  • Absolutely 100% @floatingpurr. – nexla Apr 11 '18 at 15:31
  • What if you include the schema in your query? (See [here](https://stackoverflow.com/questions/36753568/postgresql-tables-exists-but-getting-relation-does-not-exist-when-querying/36753702)) – floatingpurr Apr 11 '18 at 15:33
  • PS: Django does create objects in your DB (as you can see from `Product.objects.all()`). Probably it is just a problem with the raw query in the shell. – floatingpurr Apr 11 '18 at 15:38

2 Answers2

1

Gladly I found the solution via python chat in telegram.

The answer would be a raw query

I had to make it SELECT * FROM "Parser_product" < note the quotes.

Thanks to this answer

nexla
  • 434
  • 7
  • 20
0

Try running the makemigration command along with the app name. This is needed if you have models in the default app. Eg - python manage.py makemigrations parser. Then run the migrate command.

  • AFAIU, it resembles much more a problem with the raw query not a migration naming one. – floatingpurr Apr 11 '18 at 15:42
  • I mentioned this as nexla said in one of his comment that he was not able to make migrations. –  Apr 11 '18 at 15:45
  • I was not able because I already had it running, and also did the way you said, the problem was a raw query as @floatingpurr mentioned – nexla Apr 11 '18 at 15:48