foreign-keys

Rails has_one with class name and foreign key

∥☆過路亽.° 提交于 2019-12-21 03:10:43
问题 I have a Rails model which I use two has_one relations: requester and friend . When in the console I use: f = FriendRequest.all f[0].requester I get ActiveRecord::StatementInvalid: SQLite3::SQLException: no such column: users.requester_id: SELECT "users".* FROM "users" WHERE "users"."requester_id" = 4 LIMIT 1 . I don't really know how to specify a `has_one' relationship with a class name and a key which specifies the record. This is my model: class FriendRequest < ActiveRecord::Base has_one

Django suffix ForeignKey field with _id

Deadly 提交于 2019-12-20 18:07:30
问题 Suppose I have a field f in my model defined as follows as a foreign key: f = models.ForeignKey(AnotherModel) When Django syncs database, the filed created in db will be called f_id , automatically suffixed with '_id'. The problem is I want this field in db named exactly as what I defined in model, f in this case. How can I achieve that? 回答1: Well it turns out there's a keyword argument called db_column . If you want the field called 'f' in the database is just as simple as: f = models

Django suffix ForeignKey field with _id

久未见 提交于 2019-12-20 18:07:08
问题 Suppose I have a field f in my model defined as follows as a foreign key: f = models.ForeignKey(AnotherModel) When Django syncs database, the filed created in db will be called f_id , automatically suffixed with '_id'. The problem is I want this field in db named exactly as what I defined in model, f in this case. How can I achieve that? 回答1: Well it turns out there's a keyword argument called db_column . If you want the field called 'f' in the database is just as simple as: f = models

Django: “Soft” ForeignField without database integrity checks

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-20 12:53:53
问题 I have a Django project that has multiple django "apps". One of them has models to represent data coming from an external source (I do not control this data). I want my other apps to be able to have references to this "external app" but I want to avoid all the fuzz of the database integrity checks. I don't want the db to have any constraints on these "soft foreign keys". Do you know how I can code a custom field that will emulate a real Django ForeignKey without creating a hard constraint on

MySQL - Using foreign key as primary key too

蹲街弑〆低调 提交于 2019-12-20 11:09:08
问题 I have table 1 with a primary key user_id and table 2 where user_id is a foreign key. Only 1 record per user_id can exist in table 2, and no record can exist without it. QUESTION: Can user_id in table 2 be both foreign and primary key at the same time, and if yes, is it a good idea, what are pros/cons? 回答1: Yes, you can do this (and you should, from a database design point of view). However, consider what it means if user_id is the primary key on table 2. You are in effect saying that each

PostgreSQL: How to index all foreign keys?

蹲街弑〆低调 提交于 2019-12-20 09:13:48
问题 I am working with a large PostgreSQL database, and I am trying to tune it to get more performance. Our queries and updates seem to be doing a lot of lookups using foreign keys. What I would like is a relatively simple way to add Indexes to all of our foreign keys without having to go through every table (~140) and doing it manually. In researching this, I've come to find that there is no way to have Postgres do this for you automatically (like MySQL does), but I would be happy to hear

Primary & Foreign Keys in pgAdmin

醉酒当歌 提交于 2019-12-20 08:42:49
问题 I was wondering can some give me an explanation on how to assign primary and foreign keys in pgAdmin? I can't find any information online. For example...I've got a Student table with all their details (address, d.o.b. and etc.). I'm going to add a student_number to the table and make it a primary key. I just want to know how do I do that using pgAdmin? And if you may be kind to explain give me further information on using Primary Keys in postgreSQL (and pgAdmin). The same case with the

PostgreSQL: SQL script to get a list of all tables that has a particular column as foreign key

馋奶兔 提交于 2019-12-20 08:01:19
问题 I'm using PostgreSQL and I'm trying to list all the tables that have a particular column from a table as a foreign-key/reference. Can this be done? I'm sure this information is stored somewhere in information_schema but I have no idea how to start querying it. 回答1: select R.TABLE_NAME from INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE u inner join INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS FK on U.CONSTRAINT_CATALOG = FK.UNIQUE_CONSTRAINT_CATALOG and U.CONSTRAINT_SCHEMA = FK.UNIQUE_CONSTRAINT

MySQL best approach for db normalising, relationships and foreign keys

痴心易碎 提交于 2019-12-20 06:24:49
问题 There is an username/password verification step first then the database has following structure ^ is primary key * uses foreign key 1.StudentDetails table =========================================================================== ID^| Username | Password | Email | Address * | Website |Comments ====+============+==========+=============+===========+=========+========== 1 | xxxxxxxxxx | xxxxxxx | xx@xxx.xxx | 1 | http:// | text 2.Submissions table ==============================================

Strange foreign key behavior on empty tables in SQLite 3

五迷三道 提交于 2019-12-20 05:15:15
问题 I have SQLite 3 with the following setup (simplified): create table Location(LocationId integer not null, LocationCode text not null, primary key(LocationId), unique(LocationCode)); The table above is being referenced by Department: create table Department(DepartmentId integer not null, LocationId integer not null, DepartmentCode text not null, primary key(LocationId, DepartmentCode), foreign key(LocationId) references Location(LocationId)); The table above is being referenced by Child: