更新:通过更新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
答案 0 :(得分:0)
您可以尝试从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;