foreign-keys

Postgres creating a Table with an array of foreign keys

冷暖自知 提交于 2019-12-23 12:35:34
问题 I am making a table named "routes". I want it to be able to contain a list of flights in it. The flights' details are in the flights tables. I want "flight" to be an array of foreign key ids from the flights table. So, I have this code: CREATE TABLE routes ( id SERIAL PRIMARY KEY, flight integer[] ELEMENT REFERENCES flights, user CHARACTER VARYING(50) ); But, it gives the error: ERROR: syntax error at or near "ELEMENT" LINE 2: id SERIAL PRIMARY KEY, flight integer[] ELEMENT REFERENC... I am

Does CakePHP handle FKs on the code level or should I be adding FKs to my db too?

喜你入骨 提交于 2019-12-23 12:28:12
问题 Will I suffer consequences later if I add FKs with ON DELETE CASCADE and such? If not, what naming convention should I be using for FKs within MySQL for CakePHP? 回答1: You can see the naming conventions laid out here. Cake handles FK/relationships in the code, based on your model associations and implied associations by naming conventions. You can add an extra layer of "enforcement" by defining FK relationships on the database level. If your database honours these, it's harder to shoot

ERROR 1452: Cannot add or update a child row

懵懂的女人 提交于 2019-12-23 12:13:46
问题 I'm having a bit of a strange problem, I'm trying to add a foreign key to one table that references another, but it is failing for some reason. With my limited knowledge of MySQL, the only thing that could possibly be suspect is that there is a foreign key on a different table referencing the one I am trying to reference. Here is a picture of my table relationships, generated via workbench: Relationships CREATE TABLE `beds` ( `bedId` int(11) NOT NULL, `wardId` int(11) DEFAULT NULL, `depId`

Error:“Key … is not present in table”

邮差的信 提交于 2019-12-23 10:24:02
问题 I have a table with a character varying(12) field in it which is its PRIMARY KEY. I ran this query SELECT * FROM bg WHERE bg_id ='470370111002' It selects a row from the table. All looks good. Then I try. INSERT INTO csapp_center_bgs(bg_id,center_id) VALUES('470370111002',2) There is a foreign key on bg_id that looks like... ALTER TABLE csapp_center_bgs ADD CONSTRAINT csapp_center_bgs_bg_id_65c818f360c84dc5_fk_bg_bg_id FOREIGN KEY (bg_id) REFERENCES tiger.bg (bg_id) MATCH SIMPLE ON UPDATE NO

Foreign key in main database referencing attached database

ぐ巨炮叔叔 提交于 2019-12-23 09:58:37
问题 Is there any way in SQLite3 to have a foreign key in the main database that references columns in an attached database (or vice-versa?) I hope to share the attached (read-only) database between multiple processes, each of which has its own (read/write) main database. I create the parent table like this (in database 'ParentDB'): create table Parent (id integer primary key); Now I try this in the main database: attach 'parent.sqlite3' as ParentDB; create table Child (id integer not null

Foreign key in rails 4

独自空忆成欢 提交于 2019-12-23 09:36:38
问题 I'm using Rails 4 and SQLite. I'm trying to add foreing keys to my indicators table. Please see my code below class Indicator < ActiveRecord::Base belongs_to :objetive belongs_to :responsible, class_name: "Person" end The migration script: class AddFksToIndicator < ActiveRecord::Migration def change add_reference :indicators, :objective, index: true add_reference :indicators, :responsible, index: true end end when run the migration all is ok, so I tried in console: 2.0.0p247 :002 > i =

Validate inlines before saving model

被刻印的时光 ゝ 提交于 2019-12-23 09:32:03
问题 Let's say I have these two models: class Distribution(models.Model): name = models.CharField(max_length=32) class Component(models.Model): distribution = models.ForeignKey(Distribution) percentage = models.IntegerField() And I'm using a simple TabularInline to show Component s inside the Distribution admin form: class ComponentInline(admin.TabularInline): model = Component extra = 1 class DistributionAdmin(admin.ModelAdmin): inlines = [ComponentInline] So, my goal is to validate if the

MySQL Error creating foreign key on <ColumName> (check data types)

吃可爱长大的小学妹 提交于 2019-12-23 07:49:40
问题 I seem to be having trouble setting up a Foreign Key between two of my tables. Here is the CREATE clause for each table: CREATE TABLE IF NOT EXISTS `dbname`.`CallRecord` ( `id` INT NOT NULL AUTO_INCREMENT, `user_id` INT NOT NULL, `city_id` INT NOT NULL, `created` DATETIME NULL, `timestamp` TIMESTAMP NULL, PRIMARY KEY (`id`), INDEX `user_id_fk_idx` (`user_id` ASC), INDEX `city_id_fk_idx` (`city_id` ASC), CONSTRAINT `user_id_fk` FOREIGN KEY (`user_id`) REFERENCES `dbname`.`User` (`id`) ON

PostgreSQL : Transaction and foreign key problem

匆匆过客 提交于 2019-12-23 07:49:33
问题 I'm inserting a value in table A, that has a serial type as primary key. I wanna use the returned value of the query as a foreign key of table B... but I get this message: ERROR: insert or update on table "tb_midia_pessoa" violates foreign key constraint "tb_midia_pessoa_id_pessoa_fkey" DETAIL: Key (id_pessoa)=(30) is not present in table "tb_pessoa". ) How can I make this possible without: - starting a new Transaction - droping my foreign keys constraints =O ? Regards! Pedro 回答1: You can

set null value in a foreign key column?

只谈情不闲聊 提交于 2019-12-23 07:22:23
问题 I have this table CREATE TABLE [dbo].[CityMaster]( [CityID] [int] NOT NULL, [City] [varchar](100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL, [BranchId] [varchar](10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL CONSTRAINT [DF__CityM__Branc__74444068] DEFAULT ((0)), [ExternalBranchId] [varchar](max) COLLATE SQL_Latin1_General_CP1_CI_AS NULL, CONSTRAINT [PK_CityMaster] PRIMARY KEY CLUSTERED ( [City] ASC, [BranchId] ASC ), CONSTRAINT [uk_citymaster_cityid_branchid] UNIQUE NONCLUSTERED (