sql for Many To Many Field in existing django model

418 views
Skip to first unread message

Nikhil Verma

unread,
Mar 22, 2012, 6:55:16 AM3/22/12
to django...@googlegroups.com
Hi All

I want to add a ManyToManyField  to an existing django model.

I can use sql app_name and see the statement.

My models

Class Visit(modes.Model):
  x = something
  . ......
  ...... and so on
 # finally Here i want to add that field research protol
 research_protocol      = models.ManyToManyField(ResearchProtocol,blank=True)



class ResearchProtocol(models.Model):
    title = models.CharField(max_length=30)
    description = models.TextField()
    start_date = models.DateField(_("Date Started"))
    end_date = models.DateField(_("Date Completed"))
   
    def __unicode__(self):
        return '%s' % self.title



So i made an sql statement like this:-

CREATE TABLE "visit_visit_research_protocols"
(
    "id" integer NOT NULL PRIMARY KEY,
   
    "visit_id" integer NOT NULL REFERENCES "visit_visit" ("id") DEFERRABLE INITIALLY DEFERRED,
    "research_protocols_id" varchar(80) NOT NULL REFERENCES "www_researchprotocol" ("title") DEFERRABLE INITIALLY DEFERRED,
    UNIQUE ("visit_id","research_protocols_id")
);

The dbshell says:-
psql:db/2012-03-22_research_id.sql:8: NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "visit_visit_research_protocols_pkey" for table "visit_visit_research_protocols"
psql:db/2012-03-22_research_id.sql:8: NOTICE:  CREATE TABLE / UNIQUE will create implicit index "visit_visit_research_protocol_visit_id_research_protocols_i_key" for table "visit_visit_research_protocols"
psql:db/2012-03-22_research_id.sql:8: ERROR:  there is no unique constraint matching given keys for referenced table "www_researchprotocol"



What mistake i am doing  ?

Thanks in advance.


--
Regards
Nikhil Verma
+91-958-273-3156

Joel Goldstick

unread,
Mar 22, 2012, 8:56:55 AM3/22/12
to django...@googlegroups.com
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/django-users?hl=en.

You can't add the many to many relationship after you already have the
model with django. See this:
http://stackoverflow.com/questions/830130/adding-a-field-to-an-existing-django-model
That answer points to South, which I have played with but am not
proficient yet. Another thought is to create a new model, identical
to your present one, but include the many2many and dbsync that. If it
works, copy your data from your old model into your new model.


--
Joel Goldstick

Nikhil Verma

unread,
Mar 24, 2012, 9:02:35 AM3/24/12
to django...@googlegroups.com
Hi

I am able to create the field through in postgres.
When i execute that sql statement :-

CREATE TABLE "visit_visit_research" (
    "id" serial NOT NULL PRIMARY KEY,

    "visit_id" integer NOT NULL REFERENCES "visit_visit" ("id") DEFERRABLE INITIALLY DEFERRED,
    "research_id" integer NOT NULL REFERENCES "www_researchprotocol" ("id") DEFERRABLE INITIALLY DEFERRED,
    UNIQUE ("visit_id", "research_id")
)
;

I get this :-

psql:db/2012-03-24_research_protocol.sql:7: NOTICE:  CREATE TABLE will create implicit sequence "visit_visit_research_id_seq" for serial column "visit_visit_research.id"
psql:db/2012-03-24_research_protocol.sql:7: NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index "visit_visit_research_pkey" for table "visit_visit_research"
psql:db/2012-03-24_research_protocol.sql:7: NOTICE:  CREATE TABLE / UNIQUE will create implicit index "visit_visit_research_visit_id_key" for table "visit_visit_research"
CREATE TABLE

So the field gets created but when i am going in django-admin in table visit i am getting an error.It is not showing up two boxes i mean the many to many widget which django displays.

This is the error

Exception Type: DatabaseError at /admin/visit/visit/20/
Exception Value: column visit_visit_research.researchprotocol_id does not exist
LINE 1: ...visit_research" ON ("www_researchprotocol"."id" = "visit_vis...
                                                             ^

Any help will be appreciated.

Thanks in advance.
Reply all
Reply to author
Forward
0 new messages