提问者:小点点

德扬戈。db。乌提尔斯。编程错误:为表“products\u customer”的列“id”指定了多个默认值


在进行迁移之后,我尝试进行迁移,但是我得到了django。db。乌提尔斯。编程错误:为表“products\u customer”的列“id”指定了多个默认值

但我会从模型中删除此客户表。我怎么解决这个问题?

models.py

from django.db import models
from django.contrib.auth.models import User
from django.db.models.signals import post_save
from django.dispatch import receiver



# Create your models here.

class product(models.Model):
    product_id = models.IntegerField(primary_key=True)
    product_name = models.CharField(max_length=255)
    product_price = models.FloatField()
    product_stock = models.IntegerField()
    product_image = models.CharField(max_length=2500)



class order(models.Model):
    order_id = models.IntegerField(primary_key=True)
    product_id = models.ForeignKey(product,on_delete=models.CASCADE)
    user = models.ForeignKey(User,on_delete=models.CASCADE)
    order_quantity = models.IntegerField()
    order_total = models.IntegerField()
    order_shipped = models.BooleanField()


class address(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    house_name = models.CharField(max_length=255, blank=True)
    town = models.CharField(max_length=255, blank=True)
    post_office = models.CharField(max_length=255, blank=True)
    pincode = models.CharField(max_length=255)
    district = models.CharField(max_length=255, blank=True)
    land_mark = models.CharField(max_length=255, blank=True)
    phone = models.CharField(max_length=15, blank=True)

@receiver(post_save, sender=User)
    def create_user_address(sender, instance, created, **kwargs):
        if created:
            address.objects.create(user=instance)

@receiver(post_save, sender=User)
def save_user_address(sender, instance, **kwargs):
    instance.address.save()

错误消息

PS C:\用户\sujit\OneDrive\桌面\项目\django

要执行的操作:应用所有迁移:管理、身份验证、内容类型、产品、运行迁移的会话:应用产品。0004_自动_20211114_2342。。。回溯(最近一次调用):文件“C:\Users\sujit\AppData\Local\Programs\Python39\lib\site packages\django\db\backends\utils.py”,第84行,在执行返回self中。光标。执行(sql,params)psycopg2。错误。SyntaxError:为表“products\u customer”的列“id”指定了多个默认值

上述异常是以下异常的直接原因:Traceback(最近的最后一次调用):File"C:\用户\sujit\OneDrive\Desktop\project\django\manage.py",第22行,在main()File"C:\用户\sujit\OneDrive\Desktop\project\django\manage.py",第18行,在主execute_from_command_line(sys.argv)文件"C:\用户\sujit\AppData\本地\程序\Python\Python39\lib\site-包\django\core\management_init_. py",第419行,在execute_from_command_lineutility.execute()文件"C:\用户\sujit\AppData\本地\在执行self.fetch_command(子命令)中的程序\Python\Python39\lib\site-包\django\core\management_init_. py",第413行。run_from_argv(self.argv)File"C:\用户\sujit\AppData\本地\程序\Python\Python39\lib\site-包\django\core\管理\base.py",第354行,在run_from_argvself.execute(*args,**cmd_options)文件"C:\用户\sujit\AppData\本地\程序\Python\Python39\lib\site-包\django\core\管理\base.py",第398行,在执行输出=self.handle(*args,**选项)文件"C:\用户\sujit\AppData\本地\程序\Python\Python39\lib\site-包\django\core\管理\base.py",第89行,在包装res=handle_func(*args,**kwargs)文件"C:\用户\sujit\AppData\本地\程序\Python\Python39\lib\site-包\Django\core\管理\命令\migrate.py",第244行,在句柄post_migrate_state=executor.migrate(File"C:\用户\sujit\AppData\本地\程序\Python\Python39\lib\site-包\django\db\迁移\executor.py",第117行,处于迁移状态=_migrate_all_forwards(状态,计划,full_plan,假=假,fake_initial=fake_initial)文件"C:\用户\sujit\AppData\本地\程序\Python\Python39\lib\site-包\django\db\迁移\executor.py",第147行,_migrate_all_forwards状态=self.apply_migration(状态,迁移,假=假,fake_initial=fake_initial)文件"C:\用户\sujit\AppData\本地\程序\Python\Python39\lib\site-包\django\db\迁移\executor.py",第227行,apply_migration状态=migration.apply(状态,schema_editor)文件"C:\用户\sujit\AppData\本地\程序\Python\Python39\lib\site-包\django\db\迁移\migration.py",第126行,在应用operation.database_forwards(自己。app_label,schema_editor,old_state,project_state)文件"C:\用户\sujit\AppData\本地\程序\Python\Python39\lib\site-包\django\db\迁移\操作\field. py",第104行,在database_forwardsschema_editoradd_field中(文件"C:\用户\sujit\AppData\本地\程序\Python\Python39\lib\site-包\django\db\后端\base\schema. py",第522行,add_field自己执行(sql,params)文件"C:\用户\sujit\AppData\本地\程序\Python\Python39\lib\site-包\django\db\后端\base\schema. py",第145行,在执行cursors中。执行(sql,params)文件“C:\用户\sujit\AppData\本地\程序\Python\Python39\lib\site-包\django\db\后端\utils. py”,第98行,在执行返回超级()。执行(sql,params)文件”C:\用户\sujit\AppData\本地\程序\Python\Python39\lib\site-包\django\db\backend\utils. py",第66行,在执行返回自己。_execute_with_wrappers(sql,params,许多=False,执行器=自己。_execute)文件"C:\用户\sujit\AppData\本地\程序\Python\Python39\lib\site-包\django\db\backend\utils. py",第75行,在_execute_with_wrappers返回执行器(sql,params,许多,上下文)文件"C:\用户\sujit\AppData\本地\程序\Python\Python39\lib\site-包\django\db\backend\utils. py",第84行,在_execute返回自己. cursors.执行(sql,params)文件"C:\用户\sujit\AppData\本地\程序\Python\Python39\lib\site-包\django\db\utils. py",第90行,在退出dj_exc_valuewith_traceback(回溯)中,从exc_value文件"C:\用户\sujit\AppData\本地\程序\Python\Python39\lib\site-包\django\db\backend\utils. py",第84行,_execute返回自己. cursors.执行(sql,params)django. db. utils。编程错误:为表"products_customer"的列"id"指定多个默认值


共1个答案

匿名用户

我认为出现此错误是因为您试图在不存在的数据库表上进行迁移。

尝试删除迁移文件夹中的所有内容(除了\uuuu init\uuuuuuuy.py),然后删除。sqlite3数据库文件,然后重新运行

python manage.py makemigrations

python manage.py migrate

请注意,这将删除您的数据库和迁移到目前为止!

但很可能它会解决你的问题。