foreign-keys

Drop foreign key only if it exists

女生的网名这么多〃 提交于 2020-01-22 08:56:11
问题 I'm on a MySQL database. I'm doing this, but it doesn't work. ALTER TABLE `object` DROP FOREIGN KEY IF EXISTS `object_ibfk_1`; I've tried to put this IF EXISTS wherever I could. How can check if foreign key is exists before drop it? 回答1: If you want to drop foreign key if it exists and do not want to use procedures you can do it this way (for MySQL) : set @var=if((SELECT true FROM information_schema.TABLE_CONSTRAINTS WHERE CONSTRAINT_SCHEMA = DATABASE() AND TABLE_NAME = 'table_name' AND

How to fix foreign key error when running migration

放肆的年华 提交于 2020-01-22 02:31:08
问题 I don't know why it's still don't work and show this: Illuminate\Database\QueryException : SQLSTATE[HY000]: General error: 1005 Can't create table db_rocnikovka . books (errno: 150 "Foreign key constraint is incorrectly formed") (SQL: alter table books add constraint books_doba_foreign foreign key ( doba ) references druh_knihies ( id_druhu )) at C:\xampp\htdocs\Rocnikovka\vendor\laravel\framework\src\Illuminate\Database\Connection.php:665 Exception trace: 1 PDOException::("SQLSTATE[HY000]:

Drop foreign keys generally in POSTGRES

喜你入骨 提交于 2020-01-21 06:51:07
问题 How can I drop Foreign keys in general. I mean, if I have many foreign key constraints in a table. like MonthlyEvaluatedBudgetTable Contraints: budgetid_pk (Primary Key) branchid_fk (Foreign Key) accountid_fk (Foreign Key) dept_fk (Foreign Key) Is there a way in postgres to drop all foreign keys in general and not specifically in an existing table? Im using this line of code to drop a foreign key in an existing table. ALTER TABLE "public"."monthlyevaluatedbudgettable" DROP CONSTRAINT

Show constraints on tables command

廉价感情. 提交于 2020-01-18 11:08:23
问题 I have tables that I've tried setting PK FK relationships on but I want to verify this. How can I show the PK/FK restraints? I saw this manual page, but it does not show examples and my google search was fruitless also. My database is credentialing1 and my constrained tables are practices and cred_insurances . 回答1: I use SHOW CREATE TABLE mytable; This shows you the SQL statement necessary to receate mytable in its current form. You can see all the columns and their types (like DESC ) but it

Do all relationships modeled in SQLAlchemy have to be bi-directional?

早过忘川 提交于 2020-01-17 06:44:49
问题 I am learning python and sqlalchemy and modelled this relationship between shops and locale. I get the error: InvalidRequestError: One or more mappers failed to initialize - can't proceed with initialization of other mappers. Triggering mapper: 'Mapper|Shop|shop'. Original exception was: Mapper 'Mapper|Locale|locale' has no property 'shop' when I try to retrieve a lolcale from the db. from sqlalchemy import Column, ForeignKey, PrimaryKeyConstraint, String from sqlalchemy.orm import

Force Hibernate to create foreign key with ON DELETE CASCADE

余生长醉 提交于 2020-01-17 01:43:08
问题 When Hibernate (or another JPA implementation) creates foreign key for @OneToMany relationship, is there a way to force it to use ON DELETE CASCADE at the database level? I found that when I use CascadeType.DELETE, Hibernate doesn't do it at the database level, but sends two delete statements (for parent and child records) instead. Or maybe there's a good reason for that? 回答1: In hibernate, you can use @OnDelete(action = OnDeleteAction.CASCADE) on your @OneToMany relationship. This tells

Foreign keys in Entity Framework

Deadly 提交于 2020-01-16 18:19:52
问题 I have a method for inserting in database. I have 1:n relationship between the tables Point (n) and Line (1). Point has foreign key idLine . However, the class Point in Entity Framework doesn't have the idLine property. Now, in my method I have object of type Point , as parameter and here I have a problem because I don't know to what to assign the idSection of the new Point object which is inserted in the table. How to add the idLine property in the Point class? 回答1: I'll assume you're using

Better way to add extra attributes in RBAC (Role Based Access Control) tables?

断了今生、忘了曾经 提交于 2020-01-16 08:33:33
问题 I have implemented RBAC (Role Based Access Control) tables. I have a requirement to add extra attribute on some roles, like 2nd_pwd attribute in Admin role, address attribute in Customer role. I have done this design, but it violates data integrity. When you removed (let's say) Admin role, you also need to remove admin data from Admin table. So I decided to add trigger for this job. Question: Is there a design for this requirement without trigger but still doesn't violate data integrity? What

Understanding Primary Key and Relationship Databases with MySQL (phpmyadmin)

情到浓时终转凉″ 提交于 2020-01-16 04:01:47
问题 Forewarning - Something could be wrong from the start. More than one thing. Any additional help outside of the question I ask would be much appreciated :) Also, admin_tbl can be ignored. It is simply usernames/passwords for administrators for the site. Aim (and question): To create two relationship tables. Using my HTML Form I want to be able to insert information such as: albumName - Exploration name - Dead Sea timeDate - 2015-12-21 comment - A beautiful landscape of the Dead Sea in Israel.

How to delete rows with bi-directional dependencies?

萝らか妹 提交于 2020-01-15 19:31:28
问题 I'm using Oracle 10g Express and trying to delete records from tables with bi-directional constraints. I'm trying to un-thread hundreds of tables and dependencies generated via Hibernate (which can't be changed at this point), but here is an extremely simplified example: create table TableA (id number(19,0) not null, ..., rTableA_id number(19,0), primary key (id)); create table TableB (id number(19,0) not null, ..., rTableB_id number(19,0), primary key (id)); alter table TableA add constraint