2012-08-28 2 views
6

Ho scritto un paio di test per davvero semplice blog app, ma la relazione molti a molti non riesce quando si esegue il test: ./manage.py test myblogDjango test: DatabaseError: tale tavolo per ManyToManyField

DatabaseError: no such table: myblog_post_tag 

Eppure, quando faccio ./manage.py sql myblog:

BEGIN; 
CREATE TABLE "myblog_tag" (
    "id" integer NOT NULL PRIMARY KEY, 
    "name" varchar(50) NOT NULL 
) 
; 
CREATE TABLE "myblog_post_tag" (
    "id" integer NOT NULL PRIMARY KEY, 
    "post_id" integer NOT NULL, 
    "tag_id" integer NOT NULL REFERENCES "myblog_tag" ("id"), 
    UNIQUE ("post_id", "tag_id") 
) 
; 
CREATE TABLE "myblog_post" (
    "id" integer NOT NULL PRIMARY KEY, 
    "title" varchar(200) NOT NULL, 
    "pub_date" datetime NOT NULL, 
    "content" text NOT NULL 
) 
; 
COMMIT; 

lo fa creare una tabella, ma non riesce a farlo durante il test? Qualsiasi aiuto è apprezzato. Ecco la mia prova:

class TagModelTest(TestCase): 

    def test_create_tags_for_posts(self): 
     # tests tagging posts, postodd will have tags 1 & 3, posteven will be 2 & 4 
     postodd = Post(
      title="testing odd tags", 
      pub_date=timezone.now(), 
      content='''hello everybody, we are testing some tagging 
       functionality here. This post should have odd tags.''', 
     ) 
     posteven = Post(
      title="test even tags", 
      pub_date=timezone.now(), 
      content ='''hello everybody, we are testing some tagging 
       functionality here. This post should have even tags.''', 
     ) 
     #save them to db 
     postodd.save() 
     posteven.save() 

     # create the tags 
     tag1 = Tag(name="1") 
     tag2 = Tag(name="2") 
     tag3 = Tag(name="3") 
     tag4 = Tag(name="4") 

     # save all tags to db 
     tag1.save() 
     tag2.save() 
     tag3.save() 
     tag4.save() 

     # create the many2many relationship 
     postodd.tag.add(tag1) 

E il mio models.py se necessario:

from django.db import models 


class Tag(models.Model): 
    name = models.CharField(max_length=50) 

    def __unicode__(self): 
     return self.name 


class Post(models.Model): 
    tag = models.ManyToManyField(Tag) 
    title = models.CharField(max_length=200) 
    pub_date = models.DateTimeField(verbose_name="Date published") 
    content = models.TextField() 

    def __unicode__(self): 
     return self.title 

risposta

1

./manage.py sql myblog non esegue il codice SQL, emette proprio quello che sarebbe eseguito se è stato eseguito syncdb.

In questo caso, sembra che la tabella manchi dal tuo db.

Se questo è il risultato di una modifica a un'app esistente; per esempio hai appena aggiunto un nuovo campo al tuo modello; quindi l'esecuzione di syncdb non influirà sulle modifiche al database. syncdb non esegue alcuna operazione distruttiva (come l'aggiunta o l'eliminazione di tabelle o colonne).

In questo caso è possibile eseguire manualmente la query per aggiungere la colonna; oppure rilascia e ricrea le tue tabelle con syncdb.

Poiché questo è un problema comune, molte persone utilizzano uno strumento di migrazione dei dati come south per gestire queste modifiche. South gestirà questi piccoli cambiamenti in modo intelligente.

+2

Ma non sto usando syncdb. Al momento sto facendo solo dei test, quindi quando eseguo un test, non crea comunque un database di test? – binarymac