I thought I created a fairly simple models for my Django app, but for some reason I cannot add m2m fields to Company model objects. Here is the stacktrace
Snippet from models.py
class Contact(models.Model):
name = models.CharField(max_length=255)
email = models.CharField(max_length=255)
phone = models.CharField(max_length=255)
notes = models.TextField()
job_title = models.CharField(max_length=255)
class Company(models.Model):
name = models.CharField(max_length=255)
slug = models.SlugField(max_length=125)
contacts = models.ManyToManyField(Contact, related_name='c+')
Snippet from inspectdb
class WorkCompany(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=255)
slug = models.CharField(max_length=125)
class Meta:
managed = False
db_table = 'work_company'
class WorkCompanyContacts(models.Model):
id = models.IntegerField(primary_key=True)
company = models.ForeignKey(WorkCompany)
contacts_id = models.IntegerField()
class Meta:
managed = False
db_table = 'work_company_contacts'
class WorkContact(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=255)
email = models.CharField(max_length=255)
phone = models.CharField(max_length=255)
notes = models.TextField()
job_title = models.CharField(max_length=255)
class Meta:
managed = False
db_table = 'work_contact'
Sql Dump
BEGIN;
CREATE TABLE "work_span" (
"id" serial NOT NULL PRIMARY KEY,
"start_time" timestamp with time zone NOT NULL,
"end_time" timestamp with time zone NOT NULL
)
;
CREATE TABLE "work_task_hours" (
"id" serial NOT NULL PRIMARY KEY,
"task_id" integer NOT NULL,
"span_id" integer NOT NULL REFERENCES "work_span" ("id") DEFERRABLE INITIALLY DEFERRED,
UNIQUE ("task_id", "span_id")
)
;
CREATE TABLE "work_task" (
"id" serial NOT NULL PRIMARY KEY,
"name" varchar(255) NOT NULL,
"slug" varchar(50) NOT NULL,
"description" text NOT NULL,
"notes" text NOT NULL
)
;
ALTER TABLE "work_task_hours" ADD CONSTRAINT "task_id_refs_id_4a2c3ab2" FOREIGN KEY ("task_id") REFERENCES "work_task" ("id") DEFERRABLE INITIALLY DEFERRED;
CREATE TABLE "work_contact" (
"id" serial NOT NULL PRIMARY KEY,
"name" varchar(255) NOT NULL,
"email" varchar(255) NOT NULL,
"phone" varchar(255) NOT NULL,
"notes" text NOT NULL,
"job_title" varchar(255) NOT NULL
)
;
CREATE TABLE "work_company_contacts" (
"id" serial NOT NULL PRIMARY KEY,
"company_id" integer NOT NULL,
"contact_id" integer NOT NULL REFERENCES "work_contact" ("id") DEFERRABLE INITIALLY DEFERRED,
UNIQUE ("company_id", "contact_id")
)
;
CREATE TABLE "work_company" (
"id" serial NOT NULL PRIMARY KEY,
"name" varchar(255) NOT NULL,
"slug" varchar(125) NOT NULL
)
;
ALTER TABLE "work_company_contacts" ADD CONSTRAINT "company_id_refs_id_82bae779" FOREIGN KEY ("company_id") REFERENCES "work_company" ("id") DEFERRABLE INITIALLY DEFERRED;
CREATE TABLE "work_day_tasks" (
"id" serial NOT NULL PRIMARY KEY,
"day_id" integer NOT NULL,
"task_id" integer NOT NULL REFERENCES "work_task" ("id") DEFERRABLE INITIALLY DEFERRED,
UNIQUE ("day_id", "task_id")
)
;
CREATE TABLE "work_day" (
"id" serial NOT NULL PRIMARY KEY,
"company_id" integer NOT NULL REFERENCES "work_company" ("id") DEFERRABLE INITIALLY DEFERRED,
"hourly_rate" numeric(10, 2) NOT NULL,
"date" date NOT NULL,
"notes" text NOT NULL
)
;
ALTER TABLE "work_day_tasks" ADD CONSTRAINT "day_id_refs_id_209846d6" FOREIGN KEY ("day_id") REFERENCES "work_day" ("id") DEFERRABLE INITIALLY DEFERRED;
CREATE TABLE "work_month_work_days" (
"id" serial NOT NULL PRIMARY KEY,
"month_id" integer NOT NULL,
"day_id" integer NOT NULL REFERENCES "work_day" ("id") DEFERRABLE INITIALLY DEFERRED,
UNIQUE ("month_id", "day_id")
)
;
CREATE TABLE "work_month" (
"id" serial NOT NULL PRIMARY KEY,
"month" varchar(255) NOT NULL
)
;
ALTER TABLE "work_month_work_days" ADD CONSTRAINT "month_id_refs_id_27ea75cb" FOREIGN KEY ("month_id") REFERENCES "work_month" ("id") DEFERRABLE INITIALLY DEFERRED;
COMMIT;
Any help with this would be greatly appreciated!
Update: I removed all my south migrations and tried again, and things seem to work properly. My guess is the schema migrations left my database in a strange state. Here are the migrations that I removed to fix the problem: 0001_initial.py and 0002_auto__del_contacts__add_contact.py
I thought I created a fairly simple models for my Django app, but for some reason I cannot add m2m fields to Company model objects. Here is the stacktrace
Snippet from models.py
class Contact(models.Model):
name = models.CharField(max_length=255)
email = models.CharField(max_length=255)
phone = models.CharField(max_length=255)
notes = models.TextField()
job_title = models.CharField(max_length=255)
class Company(models.Model):
name = models.CharField(max_length=255)
slug = models.SlugField(max_length=125)
contacts = models.ManyToManyField(Contact, related_name='c+')
Snippet from inspectdb
class WorkCompany(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=255)
slug = models.CharField(max_length=125)
class Meta:
managed = False
db_table = 'work_company'
class WorkCompanyContacts(models.Model):
id = models.IntegerField(primary_key=True)
company = models.ForeignKey(WorkCompany)
contacts_id = models.IntegerField()
class Meta:
managed = False
db_table = 'work_company_contacts'
class WorkContact(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length=255)
email = models.CharField(max_length=255)
phone = models.CharField(max_length=255)
notes = models.TextField()
job_title = models.CharField(max_length=255)
class Meta:
managed = False
db_table = 'work_contact'
Sql Dump
BEGIN;
CREATE TABLE "work_span" (
"id" serial NOT NULL PRIMARY KEY,
"start_time" timestamp with time zone NOT NULL,
"end_time" timestamp with time zone NOT NULL
)
;
CREATE TABLE "work_task_hours" (
"id" serial NOT NULL PRIMARY KEY,
"task_id" integer NOT NULL,
"span_id" integer NOT NULL REFERENCES "work_span" ("id") DEFERRABLE INITIALLY DEFERRED,
UNIQUE ("task_id", "span_id")
)
;
CREATE TABLE "work_task" (
"id" serial NOT NULL PRIMARY KEY,
"name" varchar(255) NOT NULL,
"slug" varchar(50) NOT NULL,
"description" text NOT NULL,
"notes" text NOT NULL
)
;
ALTER TABLE "work_task_hours" ADD CONSTRAINT "task_id_refs_id_4a2c3ab2" FOREIGN KEY ("task_id") REFERENCES "work_task" ("id") DEFERRABLE INITIALLY DEFERRED;
CREATE TABLE "work_contact" (
"id" serial NOT NULL PRIMARY KEY,
"name" varchar(255) NOT NULL,
"email" varchar(255) NOT NULL,
"phone" varchar(255) NOT NULL,
"notes" text NOT NULL,
"job_title" varchar(255) NOT NULL
)
;
CREATE TABLE "work_company_contacts" (
"id" serial NOT NULL PRIMARY KEY,
"company_id" integer NOT NULL,
"contact_id" integer NOT NULL REFERENCES "work_contact" ("id") DEFERRABLE INITIALLY DEFERRED,
UNIQUE ("company_id", "contact_id")
)
;
CREATE TABLE "work_company" (
"id" serial NOT NULL PRIMARY KEY,
"name" varchar(255) NOT NULL,
"slug" varchar(125) NOT NULL
)
;
ALTER TABLE "work_company_contacts" ADD CONSTRAINT "company_id_refs_id_82bae779" FOREIGN KEY ("company_id") REFERENCES "work_company" ("id") DEFERRABLE INITIALLY DEFERRED;
CREATE TABLE "work_day_tasks" (
"id" serial NOT NULL PRIMARY KEY,
"day_id" integer NOT NULL,
"task_id" integer NOT NULL REFERENCES "work_task" ("id") DEFERRABLE INITIALLY DEFERRED,
UNIQUE ("day_id", "task_id")
)
;
CREATE TABLE "work_day" (
"id" serial NOT NULL PRIMARY KEY,
"company_id" integer NOT NULL REFERENCES "work_company" ("id") DEFERRABLE INITIALLY DEFERRED,
"hourly_rate" numeric(10, 2) NOT NULL,
"date" date NOT NULL,
"notes" text NOT NULL
)
;
ALTER TABLE "work_day_tasks" ADD CONSTRAINT "day_id_refs_id_209846d6" FOREIGN KEY ("day_id") REFERENCES "work_day" ("id") DEFERRABLE INITIALLY DEFERRED;
CREATE TABLE "work_month_work_days" (
"id" serial NOT NULL PRIMARY KEY,
"month_id" integer NOT NULL,
"day_id" integer NOT NULL REFERENCES "work_day" ("id") DEFERRABLE INITIALLY DEFERRED,
UNIQUE ("month_id", "day_id")
)
;
CREATE TABLE "work_month" (
"id" serial NOT NULL PRIMARY KEY,
"month" varchar(255) NOT NULL
)
;
ALTER TABLE "work_month_work_days" ADD CONSTRAINT "month_id_refs_id_27ea75cb" FOREIGN KEY ("month_id") REFERENCES "work_month" ("id") DEFERRABLE INITIALLY DEFERRED;
COMMIT;
Any help with this would be greatly appreciated!
Update: I removed all my south migrations and tried again, and things seem to work properly. My guess is the schema migrations left my database in a strange state. Here are the migrations that I removed to fix the problem: 0001_initial.py and 0002_auto__del_contacts__add_contact.py
0 commentaires:
Enregistrer un commentaire