Django, Sqlalchemy: Cannot drop table ganalytics_article because other objects depend on it

Viewed 385

I have this models in my Django ganalytics app:

class Article(models.Model):
        id = models.IntegerField(unique=True, primary_key=True)
        article_title = models.CharField(max_length=250)
        article_url = models.URLField(max_length=250)
        article_pub_date = models.DateField()
    

class Company(models.Model):
    company_name = models.CharField(max_length=250)


class Author(models.Model):
    author_sf_id = models.CharField(max_length=20, null=True)
    author_name = models.CharField(max_length=250)

  
class AuthorArticleCompany(models.Model):
    author = models.ForeignKey(Author,
                               to_field="id",
                               on_delete=models.CASCADE,
                               related_name='authorarticle_author_id')
    company = models.ForeignKey(Company,
                                to_field="id",
                                on_delete=models.CASCADE,
                                related_name='authorarticle_company_id')
    article = models.ForeignKey(Article,
                                to_field="id",
                                on_delete=models.CASCADE,
                                related_name='authorarticle_article_id')

   


class Ganalytics(models.Model):
    article = models.ForeignKey(Article,
                                on_delete=models.CASCADE,
                                related_name='ganalytics_author_id')
    totalview = models.IntegerField()
    totalinteractions = models.IntegerField()

   


class Unsubscribers(models.Model):
    email = models.EmailField()
    reasonwhy = models.CharField(max_length=90)

    

I am running pandas to_sql to upload the database:

authorarticlecompanydf.to_sql("ganalytics_authorarticlecompany", con=engine, if_exists="append", index=False)
articledf.to_sql("ganalytics_article",con=engine,if_exists="replace",index=False)
company_name.to_sql("ganalytics_company",con=engine,if_exists="replace",index=False)
authordf.to_sql("ganalytics_author", con=engine,if_exists="replace", index=False)

I am getting this error message:

DETAIL:  constraint ganalytics_ganalytic_article_id_d37f2464_fk_ganalytic on table ganalytics_ganalytics depends on table ganalytics_article
constraint ganalytics_authorart_article_id_7f4ff374_fk_ganalytic on table ganalytics_authorarticlecompany depends on table ganalytics_article
HINT:  Use DROP ... CASCADE to drop the dependent objects too.

[SQL: 
DROP TABLE ganalytics_article]

I have tried to change the on_delete field to different values but it wont help. What am I doing wrong?

1 Answers

What is the command you are running to get the error? The bottom line is Ganalytics depends on Article via the FK between them. By dropping Article first you are breaking that dependency. You either need to DROP Ganalytics first and then Article or follow the hint DROP ... CASCADE. Though be aware that will DROP Ganalytics also.

UPDATE

Another option would be to drop the FK between the two tables. Then you would not have the dependency issue.

Related