【问题标题】:During synchronization Foreign key null (error)同步期间外键空(错误)
【发布时间】:2013-08-14 15:44:12
【问题描述】:

在我的数据库中,我想同步两个表。我使用 auth_user(Django 提供的默认表)表进行注册,还有另一个表 user-profile 包含实体用户名、电子邮件、年龄等。在同步过程中如何更新外键?

def get_filename(instance,filename):
    return "upload_files/%s_%s" % (str(time()).replace('.','_'),filename)

def create_profile(sender, **kwargs):
    if kwargs["created"]:
        p = profile(username = kwargs["instance"], email=kwargs["instance"])
        p.save()

models.signals.post_save.connect(create_profile, sender=User)

class profile(models.Model):
    username = models.CharField(max_length = 30)
    email  = models.EmailField()
    age = models.PositiveIntegerField(default='15')
    picture = models.FileField(upload_to='get_filename')
    auth_user_id = models.ForeignKey(User)

在同步期间,表配置文件中的所有列都被填充,除了 auth_user_id。并且出现了错误

 Exception Value:   

(1048, "Column 'auth_user_id_id' cannot be null")

【问题讨论】:

    标签: mysql django django-models data-synchronization


    【解决方案1】:

    您必须更改表并更改允许 null 的列 auth_user_id_id 数据类型属性。

    类似这样的:-

    ALTER TABLE mytable MODIFY auth_user_id_id int;
    

    假设 auth_user_id_id 为 int 数据类型。(列默认可以为空)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-06
      • 2021-11-17
      • 1970-01-01
      • 2018-01-24
      • 1970-01-01
      • 2013-09-22
      相关资源
      最近更新 更多