foreign-keys

How to count amount of rows referring to a foreign key in MySql?

半城伤御伤魂 提交于 2019-12-07 04:51:13
问题 Let's say a table like CREATE TABLE `testdb`.`test` ( `id` bigint unsigned NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; there are other tables may have foreign key referring to test.id column. The interesting thing is I don't know what table has such a foreign key and how many rows the table has. now I want to calculate amount of rows dispersing in tables that have foreign key to test.id. Is it possible? I think it's theoretically possible, otherwise MySql

Django admin: Change selected box of related fields to autocomplete

限于喜欢 提交于 2019-12-07 04:26:26
问题 We have some models that are have a user as a foreign key. But with about 25000 users in our system, it's a bit daunting to find the one we need. Is there a solution out there that's better than the select box? Maybe an autocomplete so we can start typing the user name / address? Or just a search box? When switching the related user for these objects, it's getting harder and harder with 25000 unsorted users. Even just setting it to sort the users by username would be helpful. 回答1: I had this

MongoDB copy a field to another collection with a foreign key

岁酱吖の 提交于 2019-12-07 03:59:07
问题 I want to copy over the color from user collection to the Car collection. I'm using a foreign key which is userID. > db.test1.User.find() { "_id" : ObjectId("515f7db83f71d6bcb1c41a48"), "age" : 33, "Color" : "blue" } { "_id" : ObjectId("515f7dc03f71d6bcb1c41a49"), "age" : 52, "Color" : "red" } { "_id" : ObjectId("515f7dc43f71d6bcb1c41a4a"), "age" : 43, "Color" : "yellow" } > db.test2.Car.find() { "_id" : ObjectId("515f84883f71d6bcb1c41a54"), "speed" : 291, "userID" : ObjectId(

How can I create a foreign key on a column, every record of which may refer to a column in one of several tables?

落爺英雄遲暮 提交于 2019-12-07 02:50:21
问题 I'm in the process of creating a social network. It has several entities like news, photo, which can have comments. Since all comments have the same columns and behave the same way, and the only difference is their type — news, or photo, or something else to be added in the future — I decided to create one table for all comments with a column named type . It worked perfectly until I decided to add foreign keys to my database schema. The comment table have a column parent , which refers to id

PostgreSQL - Check foreign key exists when doing a SELECT

非 Y 不嫁゛ 提交于 2019-12-07 02:06:28
Suppose I have the following data: Table some_table : some_table_id | value | other_table_id -------------------------------------- 1 | foo | 1 2 | bar | 2 Table other_table : other_table_id | value ---------------------- 1 | foo 2 | bar Here, some_table has a foreign key to column other_table_id from other_table into the column of some name. With the following query in PostgreSQL: SELECT * FROM some_table WHERE other_table_id = 3; As you see, 3 does not exists in other_table This query obviously will return 0 results. Without doing a second query , is there a way to know if the foreign key

How to enable foreign key cascade delete by default in SQLite?

橙三吉。 提交于 2019-12-07 01:46:52
问题 SQLite v3.7.5 Is there a way to enable SQLite Foreign Keys with cascade delete enabled by default? Given the following example: CREATE TABLE [Parent] ( [ParentId] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, [Name] VARCHAR(50) UNIQUE NOT NULL ); CREATE TABLE [Child] ( [ChildId] INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, [ParentId] INTEGER NOT NULL, [Name] VARCHAR(50) NOT NULL, FOREIGN KEY(ChildId) REFERENCES Child(ParentId) ON DELETE CASCADE ); The only way I've been able to enable the

SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails - Laravel

旧巷老猫 提交于 2019-12-07 01:42:36
问题 I know that this question has already been asked, but I still can't find what I'm doing wrong. I'm using the framework Laravel. I have 2 tables (Users and Locations). When I want to create a User, I get the error message: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails ( festival_aid . users , CONSTRAINT fk_users_locations1 FOREIGN KEY ( location_id ) REFERENCES locations ( location_id ) ON DELETE CASCADE ON UPDATE NO

MySQL Error : #1005 - Can't create table (errno: 150) When I try create more than 1 FK

和自甴很熟 提交于 2019-12-07 01:33:59
问题 I have this table: CREATE TABLE IF NOT EXISTS `produtos` ( `id` int(11) NOT NULL auto_increment, `idcatprodutos` int(11) NOT NULL, `idcategoria` int(11) NOT NULL, `idmarca` int(11) NOT NULL, `nome` varchar(100) NOT NULL, PRIMARY KEY (`id`), KEY `FK_produtos_2` (`idcatprodutos`), KEY `FK_produtos_3` (`idmarca`), KEY `FK_produtos_4` (`idcategoria`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1 ROW_FORMAT=DYNAMIC AUTO_INCREMENT=39 ; and this table: CREATE TABLE IF NOT EXISTS `sugestoes` ( `id` int(11)

Django: Saving Foreign Key from Form

两盒软妹~` 提交于 2019-12-07 01:32:34
This is a continuation of this question in which I am trying to figure out how to construct a PointField composed of lat / lon FloatFields. I have taken @Simon's advice and restructured my model to look like this: class Point(models.Model): lat = models.FloatField() lon = models.FloatField() class Thing(models.Model): point = models.ForeignKey(Point) My form has two fields corresponding to values from google maps' longitude and latitude coordinates: class StepThreeForm(forms.Form): lat = forms.FloatField() lon = forms.FloatField() ... However, this does not work for obvious reasons, but I am

Mysql show create constraint?

二次信任 提交于 2019-12-07 00:09:20
问题 Is there a easy way to query a table for its constraints(foreignkeys specificaly) like show create table, but for the constraints only? thanks, pvgoddijn 回答1: To show only the foreign key constraints you can check the constraint_type in information_schema.table_constraints and get the affected columns in information_schema.key_column_usage via a join SELECT b.table_name, b.column_name, b.constraint_name, b.referenced_table_name, b.referenced_column_name FROM information_schema.table