2017-04-08 69 views
1

我不得不表中models.py:ProgrammingError:(1146,「表clearinghouse.control_experiment不存在」)

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!#$%&'*+\/=?^_`{|}~.-][email protected])(?!-.*|.*-\.)([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!#$%&'*+\/=?^_`{|}~.-][email protected])(?!-.*|.*-\.)([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 

的d這個函數調用create_experiment在maindb.py:

> 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, 「表clearinghouse.control_experiment不存在」),它來自於線experiment.save()在maindb.py。如果我刪除這一行,錯誤消失,但它當然不會將實驗保存到數據庫中。

我已經嘗試過python manage.py makemigrations和migarate,但它不是解決方案。

回答

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' 
     },  
    } 
} 
+0

它沒有。我沒有創建任何映射。謝謝 –

相關問題