CREATE TABLE ACTORS
(
ACTOR_ID NUMBER(10),
STAGE_NAME VARCHAR2(40) NOT NULL,
FIRST_NAME VARCHAR2(25) NOT NULL,
LAST_NAME VARCHAR2(25) NOT NULL,
BIRTH_DATE DATE
);
ALTER TABLE ACTORS
ADD CONSTRAINTS PK_ACTOR_ID PRIMARY KEY(ACTOR_ID);
ALTER TABLE ACTORS
ADD CONSTRAINTS FK_
FOREIGN KEY(STAR_BILLING_ID) REFERENCES STAR_BILLINGS(STAR_BILLING_ID);
CREATE TABLE STAR_BILLINGS
(
ACTOR_ID NUMBER(10) NOT NULL,
TITLE_ID NUMBER(10) NOT NULL,
STAR_BILLING_ID INT GENERATED ALWAYS AS IDENTITY,
COMMENTS VARCHAR2(40)
);
Error:
ORA-00904: "STAR_BILLING_ID": invalid identifier ORA-06512
Related
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.
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.
I am new to django .I have been following the book Practical Django Projects for learning it.
I am creating a weblog as mentioned in the book. I have followed all the steps for writing the Entry model. When the try to add an entry from the admin interface I get the error
DatabaseError at /adminlebron/entry/add/
table lebron_entry has no column named slug
After searching for a bit I found that I ran syncdb before creating the model fully and from the django documentation I found that Syncdb will not alter existing tables . After that I found South( http://south.aeracode.org/ ) and I tried using it , but i am still getting the same error.
This is my Entry Model
class Entry(models.Model):
STATUS_CHOICES = (
(1,'Live'),
(2,'Draft'),
)
title = models.CharField(max_length=250)
excerpt = models.TextField(blank=True)
body = models.TextField()
pub_date = models.DateTimeField(default=datetime.datetime.now)
slug = models.SlugField(help_text="Slug")
author = models.ForeignKey(User)
enable_comments = models.BooleanField(default=True)
featured = models.BooleanField(default=False)
status = models.IntegerField(choices=STATUS_CHOICES,default=1)
categories = models.ManyToManyField(Category)
tags = TagField()
excerpt_html = models.TextField(editable=False,blank=True)
body_html = models.TextField(editable=False,blank=True)
def save(self):
self.body_html = markdown(self.body)
if self.excerpt:
self.excerpt_html = markdown(self.excerpt)
super(Entry,self).save()
class Meta:
verbose_name_plural = "Entries"
ordering = ['-pub_date']
class Admin:
pass
def __unicode__(self):
return self.title
def get_absolute_url(self):
return "/weblog/%s/%s/" % (self.pub_date.strftime("%Y/%b/%d").lower(),self.slug)
This is the output of sqlall
BEGIN;
CREATE TABLE "lebron_category" (
"id" integer NOT NULL PRIMARY KEY,
"title" varchar(250) NOT NULL,
"slug" varchar(50) NOT NULL UNIQUE,
"description" text NOT NULL
)
;
CREATE TABLE "lebron_entry_categories" (
"id" integer NOT NULL PRIMARY KEY,
"entry_id" integer NOT NULL,
"category_id" integer NOT NULL REFERENCES "lebron_category" ("id")
UNIQUE ("entry_id", "category_id")
)
;
CREATE TABLE "lebron_entry" (
"id" integer NOT NULL PRIMARY KEY,
"title" varchar(250) NOT NULL,
"excerpt" text NOT NULL,
"body" text NOT NULL,
"pub_date" datetime NOT NULL,
"slug" varchar(50) NOT NULL,
"author_id" integer NOT NULL REFERENCES "auth_user" ("id"),
"enable_comments" bool NOT NULL,
"featured" bool NOT NULL,
"status" integer NOT NULL,
"tags" varchar(255) NOT NULL,
"excerpt_html" text NOT NULL,
"body_html" text NOT NULL
)
;
CREATE INDEX "lebron_entry_56ae2a2a" ON "lebron_entry" ("slug");
CREATE INDEX "lebron_entry_337b96ff" ON "lebron_entry" ("author_id");
COMMIT;
It appears that the column slug is there in the table after all.
I don't want to recreate the db is there any other way to correct this? The name of my app is lebron
I have two types of users in my application: auth.User, which comes from django.contrib.auth (the standard Django authentication module) and mysql.User, which is in my own module. In addition, mysql.User inherits from an abstract model. The whole thing looks similar to this (some fields were omitted for brevity):
class Resource(Model):
class Meta:
abstract = True
owners = ManyToManyField('auth.User', related_name='%(class)s_owners')
class User(Resource):
name = CharField(max_length=16)
host = CharField(max_length=64)
class Database(Resource):
name = CharField(max_length=64)
As you can see, I want to make it so that multiple auth.Users may "own" a given mysql.User and a given mysql.Database, hence the ManyToManyFields. However, when I go to run ./manage.py syncdb I get the error:
_mysql_exceptions.OperationalError: (1060, "Duplicate column name 'user_id'")
Indeed, ./manage.py sql mysql shows the source of the error (again, some columns and ALTER TABLE statements omitted for brevity):
CREATE TABLE `mysql_database` (
`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
`name` varchar(64) NOT NULL,
UNIQUE (`server_id`, `name`)
);
CREATE TABLE `mysql_user` (
`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
`name` varchar(16) NOT NULL,
`host` varchar(64) NOT NULL,
UNIQUE (`server_id`, `name`, `host`)
);
CREATE TABLE `mysql_database_owners` (
`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
`database_id` integer NOT NULL,
`user_id` integer NOT NULL,
UNIQUE (`database_id`, `user_id`)
);
CREATE TABLE `mysql_user_owners` (
`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
`user_id` integer NOT NULL,
`user_id` integer NOT NULL, -- <<<<< here is the conflict >>>>>
UNIQUE (`user_id`, `user_id`)
);
Notice how the intermediate table for Database is created without a naming conflict but the table for User has a conflict. I don't see where a ManyToManyField provides a way for one to provide column names in the intermediate table, but unfortunately I think that's what I need.
Another method I tried was to explicitly create the intermediate table and use the through option of ManyToManyField, like so:
class Resource(models.Model):
class Meta:
abstract = True
owners = models.ManyToManyField('auth.User', related_name='%(class)s_owners', through='Owner')
class Owner(models.Model):
user = models.ForeignKey('auth.User', related_name='Resource_owners')
resource = models.ForeignKey(Resource)
But then I get this error:
AssertionError: ForeignKey cannot define a relation with abstract class Resource
Which is to be expected with Django.
So, short of renaming mysql.User to something like mysql.DBUser, is there any way to avoid the naming conflict created by Django?
How about creating the many to many table separately, avoiding the use of the ManyToMany field? You could use a manager or method to return a list of users for a given resource and visa versa.
class Resource(models.Model):
...
class Meta:
abstract = True
def owners(self):
return ResourceOwner.objects.filter(resource=self)
class ResourceOwners(models.Model):
resource = models.ForeignKey(Resource)
owner = models.ForeignKey(User)
class Meta:
abstract = True
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?