ProgrammingError:(1146,“Table clearinghouse.control_experiment不存在”)

时间:2017-04-08 23:57:38

标签: python django django-models

我必须在models.py中使用表格:

class GeniUser(DjangoUser):
  usable_vessel_port = models.IntegerField("GeniUser's vessel port") 
  affiliation = models.CharField("Affiliation", max_length=200)
  user_pubkey = models.CharField("GeniUser's public key", max_length=2048)
  user_privkey = models.CharField("GeniUser's private key [!]", max_length=4096, null=True)
  api_key = models.CharField("API key", max_length=100, db_index=True)
  donor_pubkey = models.CharField("Donor public Key", max_length=2048)
  free_vessel_credits = models.IntegerField("Free (gratis) vessel credits", db_index=True)
  date_created = models.DateTimeField("Date added to DB", auto_now_add=True, db_index=True)
  date_modified = models.DateTimeField("Date modified in DB", auto_now=True, db_index=True)


class Experiment(models.Model):
  expe_name = models.CharField(max_length=30)
  geni_user = models.ForeignKey(GeniUser, db_index=True)
  researcher_name = models.CharField(max_length=30)
  researcher_institution_name = models.CharField(max_length=30)
  researcher_email = models.EmailField()
  researcher_address = models.CharField(max_length=64)
  irb_officer_email = models.EmailField()
  goal = models.CharField(max_length=256)

然后,一旦用户注册,我就有一个表单来注册实验。 在forms.py中:

class RegisterExperimentForm(forms.ModelForm):

  class Meta:
    model = Experiment
    exclude = ['geni_user']

  expe_name = forms.CharField(label="Experiment name", error_messages={'required': 'Enter a experiment name'}, required = True)
  researcher_name = forms.CharField(label="Researcher name", error_messages={'required': 'Enter a researcher name'}, required = True)
  researcher_address = forms.CharField(label="Name and address of researcher's home institution", error_messages={'required': 'Enter a Name and address of researchers home institution'}, required = True)
  researcher_email = forms.CharField(label="Researcher's email address", widget=forms.EmailInput(attrs={'class': 'form-control','pattern': "(?!(^[.-].*|[^@]*[.-]@|.*\.{2,}.*)|^.{254}.)([a-zA-Z0-9!#$%&'*+\/=?^_`{|}~.-]+@)(?!-.*|.*-\.)([a-zA-Z0-9-]{1,63}\.)+[a-zA-Z]{2,15}"}), error_messages={'required': 'Enter an E-mail Address'}, required = True)
  researcher_institution_name = forms.CharField(label="Name of home institution's IRB officer or contact person", error_messages={'required': 'Name of home institutions IRB officer or contact person'}, required = True)
  irb_officer_email = forms.CharField(label="Email address of of home institution's IRB officer or contact person", widget=forms.EmailInput(attrs={'class': 'form-control','pattern': "(?!(^[.-].*|[^@]*[.-]@|.*\.{2,}.*)|^.{254}.)([a-zA-Z0-9!#$%&'*+\/=?^_`{|}~.-]+@)(?!-.*|.*-\.)([a-zA-Z0-9-]{1,63}\.)+[a-zA-Z]{2,15}"}), error_messages={'required': 'Enter an E-mail Address'}, required = True)
  goal = forms.CharField(label="What is the goal of your research experiment? What do you want to find out?",widget=forms.Textarea(attrs={'class': 'form-control', 'rows':1,'placeholder': 'Enter the goal of your Experiment'}),error_messages={'required': 'Enter the goal of your research experiment'}, max_length=256, required = True)

所以在views.py中我清理这样的数据:

> def registerexperiment(request):
> 
>   context_instance = RequestContext(request)
> 
>   try:
>     user = _validate_and_get_geniuser(request)   
>   except LoggedInButFailedGetGeniUserError:
>     return _show_failed_get_geniuser_page(request)
> 
> 
>   page_top_errors = []   
>   username = user.username   

>  if request.method == 'POST':     
>     r_form = forms.RegisterExperimentForm(request.POST)#glabal data form
> 
> 
>     if r_form.is_valid(): #if r_form is valid save the data
>       ret.append("valid1")
>       
>       geni_user = user #foreign key of the experiment
>       expe_name = r_form.cleaned_data['expe_name']
>       res_name = r_form.cleaned_data['researcher_name']
>       res_address = r_form.cleaned_data['researcher_address']
>       res_email = r_form.cleaned_data['researcher_email']
>       irb = r_form.cleaned_data['researcher_institution_name']
>       irb_email = r_form.cleaned_data['irb_officer_email']
>       goal = r_form.cleaned_data['goal']
> 
>       try:
>         # we should never error here, since we've already finished validation at this point.
>         # but, just to be safe...
>         experiment = interface.register_experiment(geni_user,expe_name,res_name,res_address,res_email,irb, irb_email, goal)
>       except ValidationError, err:
>         page_top_errors.append(str(err))
>       else:
>          if page_top_errors == []: #all data have been saved succesfully
>            return HttpResponseRedirect(reverse("help"))
>  else:
>    r_form = forms.RegisterExperimentForm()
>
>  return render(request, 'control/registerexperiment.html', {'username' : username 'r_form': r_form, 'ret': ret, 'page_top_errors':page_top_errors})

所以这一行:

> experiment = interface.register_experiment( geni_user,expe_name,res_name,res_address,res_email,irb,
> irb_email, goal)

在interface.py中调用此函数:

> def register_experiment( geni_user,experiment_name,researcher_name,researcher_address
> ,researcher_email, irb_name,irb_email, experiment_goal):

>   validations.validate_register_experiment_field(experiment_name) 
>   validations.validate_register_experiment_field(researcher_name)`  
>   validations.validate_register_experiment_field(researcher_address)  
>   validations.validate_email(researcher_email)  
>   validations.validate_register_experiment_field(irb_name)  
>   validations.validate_email(irb_email)  
>   validations.validate_register_experiment_field(experiment_goal)
> 
>   experiment = maindb.create_experiment(geni_user,
> experiment_name,researcher_name,researcher_address ,researcher_email,
> irb_name,irb_email, experiment_goal)
> 
>   return experiment

此函数在maindb.py中调用create_experiment:

> def create_experiment( geni_user,experiment_name,researcher_name,researcher_address
> ,researcher_email, irb_name,irb_email, experiment_goal):
> 
>   assert_str(experiment_name)   assert_str(researcher_name)  
> assert_str(researcher_address)   assert_str(researcher_email)  
> assert_str(irb_name)   assert_str(irb_email)  
> assert_str(experiment_goal)
> 
>   # We're committing manually to make sure the multiple database
> writes are   # atomic. (That is, regenerate_api_key() will do a
> database write.)   try:
>       with transaction.atomic():
>           # Create the Experiment
>           experiment = Experiment(expe_name=experiment_name, geni_user=geni_user, 
>                               researcher_name=researcher_name, researcher_institution_name = irb_name,
>                               researcher_email=researcher_email, researcher_address=researcher_address,
>                               irb_officer_email=irb_email, goal=experiment_goal)   
>           experiment.save()   except:
>     transaction.rollback()
>     raise
>      else:
>     transaction.commit()
> 
>   return experiment

我遇到的错误是ProgrammingError:(1146,“Table clearinghouse.control_experiment不存在”),它来自maindb.py中的experiment.save()行。如果我删除此行,则错误消失,但当然也不会将实验保存到数据库中。

我已经尝试过python manage.py makemigrations和migarate但它不是解决方案。

1 个答案:

答案 0 :(得分:1)

Experiment模型是否映射到clearinghouse.control_experiment表?

如果是这样,你如何创建该映射?您的模型Experiment没有任何显示数据库中使用的表名和模式的Meta类。

现在,Django不支持postgres架构。默认情况下,Django只能"看到" public架构中的表。要查看除public之外的其他模式,您必须使用选项打开连接:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'mydatabase',
        'USER': 'myuser',
        'PASSWORD': 'mypassword',
        'HOST': 'localhost',
        'PORT': '5432',
        'OPTIONS': {
            'options': '-c search_path=public,clearinghouse,otherschema'
        },       
    }
}