I am making a Django App and having the following issue when I am trying to add something to the table 'device' of my app from the admin interface:
column device.plID_id does not exist
Here is class:
class Device(models.Model):
id = models.IntegerField(primary_key=True)
name = models.CharField(max_length = 20)
slug = models.SlugField(unique=True,help_text='URL page created from name')
model_name = models.CharField(max_length = 20)
plID = models.ForeignKey('onep_web.Platform', unique = False)
sdkID = models.ForeignKey('onep_web.SDKVersion',unique = False)
IPID = models.ForeignKey('onep_web.IPAddress',unique = False)
and output of sqall command:
BEGIN;
CREATE TABLE "device" (
"id" integer NOT NULL PRIMARY KEY,
"name" varchar(20) NOT NULL,
"slug" varchar(50) NOT NULL UNIQUE,
"model_name" varchar(20) NOT NULL,
"plID_id" integer NOT NULL,
"sdkID_id" integer NOT NULL,
"IPID_id" integer NOT NULL
)
;
CREATE TABLE "platform" (
"id" integer NOT NULL PRIMARY KEY,
"platform_name" varchar(20) NOT NULL
)
;
ALTER TABLE "device" ADD CONSTRAINT "plID_id_refs_id_364a8522" FOREIGN KEY ("plID_id") REFERENCES "platform" ("id") DEFERRABLE INITIALLY DEFERRED;
CREATE TABLE "sdk_ver" (
"id" integer NOT NULL PRIMARY KEY,
"language" varchar(20) NOT NULL,
"version" numeric(100, 100) NOT NULL
)
;
ALTER TABLE "device" ADD CONSTRAINT "sdkID_id_refs_id_bb71f431" FOREIGN KEY ("sdkID_id") REFERENCES "sdk_ver" ("id") DEFERRABLE INITIALLY DEFERRED;
CREATE TABLE "ip_addr" (
"id" integer NOT NULL PRIMARY KEY,
"ip" inet NOT NULL
)
;
ALTER TABLE "device" ADD CONSTRAINT "IPID_id_refs_id_7c42b845" FOREIGN KEY ("IPID_id") REFERENCES "ip_addr" ("id") DEFERRABLE INITIALLY DEFERRED;
CREATE TABLE "files" (
"id" integer NOT NULL PRIMARY KEY,
"file" varchar(100) NOT NULL,
"isConfig" boolean NOT NULL
)
;
CREATE INDEX "device_slug_like" ON "device" ("slug" varchar_pattern_ops);
CREATE INDEX "device_plID_id" ON "device" ("plID_id");
CREATE INDEX "device_sdkID_id" ON "device" ("sdkID_id");
CREATE INDEX "device_IPID_id" ON "device" ("IPID_id");
When there were only two tables Device and Platform everything was OK. After I added a few more tables I have this issue
Django's manage.py, does not use or handles migrations (currently) you need to use south to alter tables (when you alter models):
http://south.aeracode.org/
Install south, follow the detailed instructions and you will easily maintain your schema up to date.
Related
My models:
class TestSkill(models.Model):
name = models.CharField(max_length=255)
value = models.CharField(max_length=255)
class Girl(models.Model):
skills = models.ManyToManyField(TestSkill)
this has created following sql statement:
CREATE TABLE "my_app_girl_skills" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "girl_id" integer NOT NULL REFERENCES "my_app_girl" ("id"), "testskill_id" integer NOT NULL REFERENCES "my_app_testskill" ("id"))
And after I tried manually to create a row in db using sql lite browser I 've got an error:
Foreign constraint failed
Iam working on a django project and I am configuring preconfigured database in sqlite3 to postgress locally .First i have done all my migrations to sqlite3 database all was working fine ..
After that i want to migrate to postgressql so i created a local database and connected to it ...
The model iam migrating is ::
class State(models.Model):
statename = models.CharField(db_column='statename',max_length=26,unique=True)
def __str__(self):
return self.statename
class College(models.Model):
statename= models.ForeignKey(State,db_column='statename',on_delete=models.CASCADE,to_field='statename')#used to_field to convert int to text must set unique to true for operation
collegename = models.CharField(db_column='collegename',max_length=30,unique=True)
def __str__(self):
return self.collegename
This are the two databases DDL generated by Django ::
sqlite3 working fine -----
CREATE TABLE api_state
(
id integer PRIMARY KEY AUTOINCREMENT NOT NULL,
statename varchar(25) NOT NULL
);
CREATE UNIQUE INDEX sqlite_autoindex_api_state_1 ON api_state
(statename);
CREATE TABLE api_college
(
id integer PRIMARY KEY AUTOINCREMENT NOT NULL,
collegename varchar(30) NOT NULL,
state varchar(25) NOT NULL,
FOREIGN KEY (state) REFERENCES api_state (statename)
);
CREATE UNIQUE INDEX sqlite_autoindex_api_college_1 ON api_college (collegename);
CREATE INDEX api_college_9ed39e2e ON api_college (state)
postgress ddl changed after generation with Django::
create table api_college
(
id serial not null
constraint api_college_pkey
primary key,
collegename varchar(30) not null,
statename_id integer not null
)
;
create index api_college_b291eb83
on api_college (statename_id)
;
create table api_state
(
id serial not null
constraint api_state_pkey
primary key,
statename varchar(25) not null
)
;
alter table api_college
add constraint api_college_statename_id_7e126c2d_fk_api_state_id
foreign key (statename_id) references api_state
deferrable initially deferred
;
Error that iam getting int he postgress migration ::
File "/home/hexnor/apienv/lib/python3.5/site-packages/django/utils/six.py", line 685, in reraise
raise value.with_traceback(tb)
File "/home/hexnor/apienv/lib/python3.5/site-packages/django/db/backends/utils.py", line 64, in execute
return self.cursor.execute(sql, params)
django.db.utils.DataError: value too long for type character varying(3)
Error that i got after confguring postgress database and access django admin on browser::
i have first added state UP
then when iam choosing state as Up and adding college as testcollege iam getting this error ::
Exception Type: ProgrammingError
Exception Value:
column "statename" of relation "api_college" does not exist
LINE 1: INSERT INTO "api_college" ("statename", "collegename") VALUE...
I have two models:
class StateTax(models.Model):
name = models.CharField(max_length=256)
abbr = models.CharField(max_length=64, primary_key=True)
rate = models.IntegerField(default=0)
class Account(models.Model):
name = models.CharField(max_length=32)
tax_regions = models.ManyToManyField(SalesTaxRegion, blank=True, null=True, related_name="accounts")
However the SQL Django creates during syncdb seems to ignore the primary_key option. For example:
CREATE TABLE `airship_salestaxregion` (
`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
`name` varchar(256) NOT NULL,
`abbr` varchar(64) NOT NULL,
`rate` integer NOT NULL
)
;
CREATE TABLE `airship_account_tax_regions` (
`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
`account_id` integer NOT NULL,
`salestaxregion_id` integer NOT NULL,
UNIQUE (`account_id`, `salestaxregion_id`)
)
;
(note there is still an ID column in the first table and the relationship table references it)
As Like It says in the comment, you're getting your tables mixed up, you've given us python code for StateTax, not SalesTaxRegion.
I've tested your StateTax code with Django 1.4 and Postgres 9.2 and I get the following -
-- DROP TABLE testing_statetax;
CREATE TABLE testing_statetax
(
name character varying(256) NOT NULL,
abbr character varying(64) NOT NULL,
rate integer NOT NULL,
CONSTRAINT testing_statetax_pkey PRIMARY KEY (abbr)
)
There's no additional id field added.
Question> Why django 1.3 ('ENGINE': 'django.db.backends.sqlite3' ) doesn't apply constraint on the tag_id attribute of table bookmarks_tag_bookmark?
Generated:
CREATE TABLE "bookmarks_tag_bookmarks" (
"id" integer NOT NULL PRIMARY KEY,
"tag_id" integer NOT NULL,
"bookmark_id" integer NOT NULL REFERENCES "bookmarks_bookmark" ("id"),
UNIQUE ("tag_id", "bookmark_id")
)
;
Expected:
CREATE TABLE "bookmarks_tag_bookmarks" (
"id" integer NOT NULL PRIMARY KEY,
"tag_id" integer NOT NULL REFERENCES "bookmarks_tag" ("id"),
"bookmark_id" integer NOT NULL REFERENCES "bookmarks_bookmark" ("id"),
UNIQUE ("tag_id", "bookmark_id")
)
;
# ----- Detail of The Coding ------------ #
user#ubuntu:~/Documents/Django/django_bookmarks$ cat bookmarks/models.py
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class Link(models.Model):
url = models.URLField(unique=True)
class Bookmark(models.Model):
title = models.CharField(max_length=200)
user = models.ForeignKey(User)
link = models.ForeignKey(Link)
class Tag(models.Model):
name = models.CharField(max_length=64, unique=True)
bookmarks = models.ManyToManyField(Bookmark)
user#ubuntu:~/Documents/Django/django_bookmarks$ python manage.py syncdb
user#ubuntu:~/Documents/Django/django_bookmarks$ python manage.py sql bookmarks
BEGIN;
CREATE TABLE "bookmarks_link" (
"id" integer NOT NULL PRIMARY KEY,
"url" varchar(200) NOT NULL UNIQUE
)
;
CREATE TABLE "bookmarks_bookmark" (
"id" integer NOT NULL PRIMARY KEY,
"title" varchar(200) NOT NULL,
"user_id" integer NOT NULL REFERENCES "auth_user" ("id"),
"link_id" integer NOT NULL REFERENCES "bookmarks_link" ("id")
)
;
CREATE TABLE "bookmarks_tag_bookmarks" (
"id" integer NOT NULL PRIMARY KEY,
"tag_id" integer NOT NULL,
"bookmark_id" integer NOT NULL REFERENCES "bookmarks_bookmark" ("id"),
UNIQUE ("tag_id", "bookmark_id")
)
;
CREATE TABLE "bookmarks_tag" (
"id" integer NOT NULL PRIMARY KEY,
"name" varchar(64) NOT NULL UNIQUE
)
;
COMMIT;
user#ubuntu:~/Documents/Django/django_bookmarks$
Because bookmarks_tag is not created at that point, so it can't define the constraint at table creation time. However if you do manage.py sqlall you will see that it later does an ALTER TABLE to add the constraint once all the tables are created.
(Although it's worth pointing out that Sqlite ignores contraints except in very recent versions, and even then only if specifically enabled.)
I have class SubForum with ForeignKey to self - parent:
class Forum(models.Model):
name = models.CharField(max_length=200)
url = models.URLField()
class SubForum(models.Model):
name = models.CharField(max_length=200)
orginal_id = models.IntegerField()
forum = models.ForeignKey('Forum')
parent = models.ForeignKey('self', null=True, blank=True)
I want to allow for null and blank enteries - I saw examples that this is a proper way to do that.
In sql view everything is ok:
BEGIN;CREATE TABLE "main_forum" (
"id" integer NOT NULL PRIMARY KEY,
"name" varchar(200) NOT NULL,
"url" varchar(200) NOT NULL
)
;
CREATE TABLE "main_subforum" (
"id" integer NOT NULL PRIMARY KEY,
"name" varchar(200) NOT NULL,
"orginal_id" integer NOT NULL,
"forum_id" integer NOT NULL REFERENCES "main_forum" ("id"),
"parent_id" integer
)
;COMMIT;
In parent_id field there is no NOT NULL, but when I want to add new SubForum using admin panel without setting parent i get error:
Cannot assign None: "SubForum.parent" does not allow null values.
What's wrong?
I made some changes, reverted it back and now everything is working fine... and I don't see any difference with code that I posted here...
Should I delete question?