以下是错误消息,我最近玩了我的models.py而且我明白了。在本地,每件事都按照我的预期工作,但有弹性豆茎我得到以下错误。
<div class="form-group field-company-company_name required has-error">
<label class="col-lg-2 control-label name-label" for="company-company_name" data-name="Operator Name">Operator Name</label>
<div class="col-lg-3">
<input type="text" id="company-company_name" class="form-control" name="Company[company_name]" required="required">
</div>
<div class="col-lg-7">
<p class="help-block help-block-error">Company Name cannot be blank.</p>
</div>
</div>
这是因为弹性beanstalk?还是我的代码有问题?
[Instance: i-491802ee] Command failed on instance. Return code: 1 Output: (TRUNCATED)...l, params) django.db.utils.IntegrityError: insert or update on table "main_category" violates foreign key constraint "main_categor_sponosred_id_1adc236d9d5dae5f_fk_main_sponsored_id" DETAIL: Key (sponosred_id)=(1) is not present in table "main_sponsored". container_command 01_migrate in .ebextensions/02_python.config failed. For more detail, check /var/log/eb-activity.log using console or EB CLI.
编辑:
class Sponsored(models.Model):
title = models.CharField(max_length=120)
description = models.TextField(max_length=5000, null=True, blank=True)
image = models.ImageField(upload_to='sponsored1/', null=True, blank=True,max_length=255)
objects = SponsoredManager()
class Category(models.Model):
sponsored = models.ForeignKey(Sponsored)
我甚至尝试添加此内容,
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import models, migrations
class Migration(migrations.Migration):
dependencies = [
('main', '0024_auto_20160328_2027'),
]
operations = [
migrations.AddField(
model_name='category',
name='sponosred',
field=models.ForeignKey(default=1, to='main.Sponsored'),
preserve_default=False,
),
]
most recent one
class Migration(migrations.Migration):
dependencies = [
('main', '0029_category_sponsored'),
]
operations = [
migrations.AlterField(
model_name='post',
name='content',
field=ckeditor_uploader.fields.RichTextUploadingField(),
),
]
在当地一切正常,因为我把赞助模型放在类别模型之前,所以不应该有任何问题。 但是当我运行eb deploy时,我收到错误....
答案 0 :(得分:1)
根据移民情况:
field=models.ForeignKey(default=1, to='main.Sponsored'),
这意味着Category
表的现有行应该对sponsored
列使用值1(因为它非空,每行必须具有此值)。
生产失败,因为Sponsored
表格中没有1
主要行sponsored
(Category
中1
为必填项)键)
您的本地数据库有一行主键sponsored = models.ForeignKey(Sponsored, null=True)
,因此它没有失败。
您可以使赞助字段可以为空并运行makemigrations&amp;再次迁移。
-keep class org.apache.commons.logging.**
答案 1 :(得分:0)
violates foreign key constraint
错误,由数据库引发。
您的Category
模型具有Sponsored
模型的外键。
如果您尝试将项目插入Category
模型,而引用的Sponsored
对象不存在,
数据库抱怨。
解决方案:
Sponsored
模型之前保存Category
对象。--natural-foreign
命令,请使用dumpdata
python manage.py dumpdata --natural-foreign