【问题标题】:IntegrityError null value in column "id" after importing DB to postgresql将数据库导入 postgresql 后,“id”列中的 IntegrityError 空值
【发布时间】:2019-01-25 21:05:27
【问题描述】:

更新:通过更新 id 序列中的 lastval 解决了这个问题

SELECT pg_catalog.setval('public.products_product_id_seq', max(id), false);

其中max(id)select max(id) from products_product

我的数据库在 mysql 上,当我尝试使用多种工具将其导入 postgresql 时,它多次失败。

我决定为每个表导出 csvs,然后使用 COPY 将它们导入 postgres。

COPY products_product(id, name, unit_price, timestamp, updated, description, 
category_id, minimum_stock, qrcode, stock, unit, owner_id) 
FROM '~/csv/products.csv' DELIMITER ';' CSV HEADER;

导入后,我可以更新我的所有模型,无论我使用什么视图来更新(我也在管理员上尝试过)。

但是,当我尝试创建一个新对象(不管是哪一个)时,我得到一个IntegrityError,例如:null value in column "id" violates not-null constraint。 (在管理员上也试过)。

我的所有模型和所有视图都存在相同的问题,这使我相信导入后我的 postgres 数据库一定有问题(可能是主键计数器?)。因为在运行迁移之后和导入任何内容之前,我能够创建对象。

我该如何解决这个问题?是否有配置或我必须在 postgres 上更改的内容,或者我不知道关于 Django 的内容?

如果你对我的模型感到好奇:

class Product(models.Model):
    name = models.CharField(
        max_length=120,
        null=False,
        blank=False,
        verbose_name='Nombre')
    description = models.TextField(
        blank=True,
        null=True,
        verbose_name='Descripción')
    category = models.ForeignKey(
        'Category',
        on_delete=models.SET_NULL,
        null=True,
        blank=True,
        verbose_name='Categoría')
    unit_price = models.PositiveIntegerField(
        default=0,
        null=False,
        blank=False,
        verbose_name='Precio unitario')
    unit = models.CharField(
        max_length=255,
        default='ud',
        blank=True)
    image = models.ImageField(
        upload_to='images/products',
        blank=True,
        null=True,
        verbose_name='Imagen')
    qrcode = models.ImageField(
        upload_to='qrcode',
        blank=True,
        null=True)
    stock = models.IntegerField(default=0)
    minimum_stock = models.PositiveIntegerField(default=0)

    owner = models.ForeignKey(
        'users.Company',
        on_delete=models.CASCADE,
        related_name='products')
    timestamp = models.DateTimeField(
        default=timezone.now,
        null=True)
    updated = models.DateTimeField(
        auto_now=True,
        null=True)

    def __str__(self):
        return self.name

我的 postgress 表是什么样子的:

Table "public.products_product"
    Column     |           Type           | Collation | Nullable |         Default         
---------------+--------------------------+-----------+----------+-------------------------
 id            | integer                  |           | not null | 
 name          | character varying(120)   |           | not null | NULL::character varying
 description   | text                     |           |          | 
 unit_price    | integer                  |           | not null | 
 unit          | character varying(255)   |           | not null | NULL::character varying
 image         | character varying(100)   |           |          | 
 qrcode        | character varying(100)   |           |          | NULL::character varying
 stock         | integer                  |           | not null | 
 minimum_stock | integer                  |           | not null | 
 timestamp     | timestamp with time zone |           |          | 
 updated       | timestamp with time zone |           |          | 
 category_id   | integer                  |           |          | 
 owner_id      | integer                  |           | not null | 1
Indexes:
    "products_product_pkey" PRIMARY KEY, btree (id)
    "products_product_category_id_9b594869" btree (category_id)
    "products_product_owner_id_f189d068" btree (owner_id)
Check constraints:
    "products_product_minimum_stock_check" CHECK (minimum_stock >= 0)
    "products_product_unit_price_check" CHECK (unit_price >= 0)
Foreign-key constraints:
    "products_product_category_id_9b594869_fk_products_category_id" FOREIGN KEY (category_id) REFERENCES products_category(id) DEFERRABLE INITIALLY DEFERRED
    "products_product_owner_id_f189d068_fk_users_company_id" FOREIGN KEY (owner_id) REFERENCES users_company(id) DEFERRABLE INITIALLY DEFERRED
Referenced by:
    TABLE "expenses_item" CONSTRAINT "expenses_item_product_id_551c230a_fk_products_product_id" FOREIGN KEY (product_id) REFERENCES products_product(id) DEFERRABLE INITIALLY DEFERRED
    TABLE "orders_item" CONSTRAINT "orders_item_product_id_260e6ee8_fk_products_product_id" FOREIGN KEY (product_id) REFERENCES products_product(id) DEFERRABLE INITIALLY DEFERRED
    TABLE "quotations_item" CONSTRAINT "quotations_item_product_id_fa055ee8_fk_products_product_id" FOREIGN KEY (product_id) REFERENCES products_product(id) DEFERRABLE INITIALLY DEFERRED

以及样本 csv 数据:

"id";"name";"unit_price";"timestamp";"updated";"description";"category_id";"minimum_stock";"qrcode";"stock";"unit";"owner_id"
1;"Tote bag";1;"2018-07-24 17:05:37.631487";"2018-12-24 17:05:37.631487";"";;0;"";0;"ud";1
4;"Canvas bag";280;"2018-07-29 22:43:58.015396";"2018-12-29 22:43:58.015396";"";;0;"";0;"ud";1
5;"T-shirt";400;"2018-07-29 22:44:39.847575";"2018-12-29 22:45:06.463699";"";;0;"";0;"ud";1

【问题讨论】:

  • 添加样本 csv 数据
  • 您可能应该为id 设置一个default(定义一个“自动调度”ID 的序列)。
  • @ShafikurRahman 刚刚添加到最后。
  • @WillemVanOnsem 我应该在模型上设置默认值吗?如何定义该序列?
  • @WillemVanOnsem 我想我明白你的意思了。我创建了一个新数据库并再次迁移,现在我确实有一个序列nextval('products_product_id_seq'::regclass)。但是我有一个新问题:计数器似乎从1开始。多次刷新页面并得到:IntegrityError: duplicate key value violates unique constraint "products_product_pkey" DETAIL: Key (id)=(1) already exists,然后key id = 2等等,我设法创建了一个产品。我会尝试设置一个默认值。

标签: django postgresql import


【解决方案1】:

您可以尝试从 postgres 复制命令中删除 id 并从 csv 中删除 id。 喜欢

COPY products_product(name, unit_price, timestamp, updated, description, 
category_id, minimum_stock, qrcode, stock, unit, owner_id) 
FROM '~/csv/products.csv' DELIMITER ';' CSV HEADER;

【讨论】:

  • 我不想尝试,因为其他型号对这些产品有ForeignKey。我可以在删除id 的同时保留这种关系吗?
  • 我不知道这个。你可以试试这个stackoverflow.com/questions/35161432/…
  • 谢谢!我去看看
猜你喜欢
  • 1970-01-01
  • 2012-10-20
  • 2017-04-05
  • 2021-06-26
  • 2016-10-16
  • 2013-07-25
  • 2020-01-19
  • 2019-10-05
  • 2011-07-22
相关资源
最近更新 更多