foreign-keys

django.db.utils.IntegrityError: FOREIGN KEY constraint failed

删除回忆录丶 提交于 2019-12-13 02:14:50
问题 My models.py class Order(models.Model): user = models.ForeignKey(User, blank=True, null=True, on_delete=models.PROTECT) customer_email = models.EmailField(blank=True, null=True, default=None) customer_name = models.CharField(max_length = 64, blank=True, null=True, default=None) customer_phone = models.CharField(max_length = 48, blank=True, null=True, default=None) customer_address = models.CharField(max_length = 128, blank=True, null=True, default=None) total_price = models.DecimalField(max

How do I choose which column from the “foreign” table is displayed in the view?

梦想的初衷 提交于 2019-12-13 01:39:14
问题 I am building a simple ASP.NET MVC app with Entity Framework Database First that allows a user to edit tables in a database. One table has a foreign key to another table. I want the user to be able to change the foreign key value. My question: How do I choose which column from the "foreign" table is displayed to the user in the view? I scaffolded the view out, but it is displaying the wrong column. The foreign key is in the DealerAuto table, which has columns: DealerAutoID, DealerID,

Foreign key matching in PostgreSQL

半城伤御伤魂 提交于 2019-12-13 01:16:01
问题 Just curious, if I have this table: CREATE TABLE "post" ( "id" SERIAL, "revision" INTEGER NOT NULL DEFAULT 0, "summary" CHARACTER VARYING NOT NULL, "description" TEXT NOT NULL, "user_id" INTEGER NOT NULL REFERENCES "user" ("id") MATCH FULL ON UPDATE CASCADE ON DELETE RESTRICT, "post_type_id" INTEGER NOT NULL REFERENCES "post_type" ("id") MATCH FULL ON UPDATE CASCADE ON DELETE RESTRICT, "ctime" TIMESTAMP WITH TIME ZONE DEFAULT NOW(), PRIMARY KEY("id", "revision") ); to store posts, and this

How to get image connected by Foreign Key via Template in django

浪子不回头ぞ 提交于 2019-12-13 01:06:33
问题 i want to get the images form the image model in the template. class Products(models.Model): category = models.ForeignKey(Category) name= models.CharField(max_length=120, unique=True) slug = models.SlugField(unique = True) price = models.IntegerField(default=100) class Image(models.Model): property = models.ForeignKey(Products, related_name='images') image = models.ImageField(upload_to='static/images/home',blank=True,null=True) views.py def index(request): queryset = Products.objects.all()

How to reference a composite primary key in SQL

本小妞迷上赌 提交于 2019-12-12 23:31:22
问题 I have created the following 3 tables using the following code. CREATE TABLE BUILDING( BUILDINGNO CHAR(2), BUILDINGWING VARCHAR2(15), BUILDINGLANE VARCHAR2(15), CONSTRAINT BUILDING_PK PRIMARY KEY(BUILDINGNO)); CREATE TABLE ROOM( BUILDINGNO CHAR(2), ROOMNO CHAR(2), ROOMCAPACITY NUMBER(3), CONSTRAINT ROOM_PK PRIMARY KEY(BUILDINGNO,ROOMNO), CONSTRAINT ROOM_FK1 FOREIGN KEY(BUILDINGNO) REFERENCES BUILDING(BUILDINGNO)); CREATE TABLE SPEAKER( SPEAKERID CHAR(2), SPEAKERNAME VARCHAR2(20),

Does using Foreign Key speed up table joins

梦想的初衷 提交于 2019-12-12 22:00:50
问题 I think my title is pretty self explainitory, however I will outline what I am doing below. A person is connected to a town. This town is connected to a county. The county is connected to a country. There are many counties in one country. Also there are many towns in a county. That being said many people can be from the same town. In my database I have a separate table for people, towns, counties and countries. To get a full user record I LEFT JOIN the tables on a record set. This is a

Yii conditional relation

痴心易碎 提交于 2019-12-12 21:39:01
问题 I have a chat table with fields admin TINYINT owner_id INTEGER The goal is to have two relations in Yii: 'admin'=>array( self::BELONGS_TO, 'Admin', 'owner_id', 'condition'=>'admin=1', ), 'user'=>array( self::BELONGS_TO, 'User', 'owner_id', 'condition'=>'admin=0', ), However, I got General error: 1 no such column: admin , and could manage only by adding all_ones and all_zeros columns to Admin table, so I could write 'admin'=>array( self::BELONGS_TO, 'Admin', array('owner_id' => 'id', 'admin' =

Django REST Framework serializer - access existing foreign key

为君一笑 提交于 2019-12-12 19:40:03
问题 I am using Django Rest Framework in my app, and I need to create new model instances which contain foreign keys. These refer to existing objects in another table, so I don't want new instances of these foreign objects to be created. Also I cannot access these objects via their primary keys, as that information is not submitted (I need to filter on certain fields which are included in the POST request). How do I do this? This question seems to address the same issue, though it's not clear to

django : unique name for object within foreign-key set

我只是一个虾纸丫 提交于 2019-12-12 19:38:00
问题 I'm trying to upload files for an article model. Since an object can have multiple images, I'm using a foreign-key from file model to my article model. However, I want all the files to have unique titles. Herez the code snippet. class Article(models.Model): name = models.CharField(max_length=64) class Files(models.Model): title = models.CharField(max_length=64) file = models.FileField(upload_to="files/%Y/%m/%d/") article = models.ForeignKey(Article) Now when I upload the files, I want the

Deleting from two h2 tables with a foreign key

我与影子孤独终老i 提交于 2019-12-12 18:54:04
问题 I have an h2 database with two tables with foreign keys like CREATE TABLE master (masterId INT PRIMARY KEY, slaveId INT); CREATE TABLE slave (slaveId INT PRIMARY KEY, something INT, CONSTRAINT fk FOREIGN KEY (slaveId) REFERENCES master(slaveId)); and need something like DELETE master, slave FROM master m JOIN slave s ON m.slaveId = s.slaveId WHERE something = 43; i.e., delete from both tables (which AFAIK works in MySql). Because of the FOREIGN KEY I can't delete from the master first. When I