我的 Django ganalytics 应用程序中有这个模型:

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)


我正在运行 pandas to_sql 来上传数据库:
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)
我收到此错误消息:
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]
我试图将 on_delete 字段更改为不同的值,但无济于事。
我究竟做错了什么?

最佳答案

您正在运行什么命令来获取错误?底线是 Ganalytics 通过它们之间的 FK 依赖于 Article。通过首先删除 Article 您打破了这种依赖关系。您需要先 DROP Ganalytics 然后 Article 或按照提示 DROP ... CASCADE 。虽然要注意 DROP Ganalytics 也会。
更新
另一种选择是删除两个表之间的 FK。那么你就不会有依赖问题。

关于Django,Sqlalchemy : Cannot drop table ganalytics_article because other objects depend on it,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/63809018/

10-11 08:31