cannot add foreign key constraint workbench - mysql-workbench

Executing the following SQL script in the server:
ERROR: Error 1215: Cannot add foreign key constraint
SQL Code:
CREATE TABLE IF NOT EXISTS `telecom`.`jobreq` (
`id` INT NOT NULL,
`jobName` VARCHAR(45) NULL,
`priority` VARCHAR(45) NULL,
`dates` DATE NULL,
`status` VARCHAR(45) NULL,
`user` VARCHAR(45) NULL,
`timestamp` DATE NULL,
`service_id` INT(11) NOT NULL,
`client_id` INT(11) NOT NULL,
PRIMARY KEY (`id`, `service_id`, `client_id`),
INDEX `fk_jobreq_service1_idx` (`service_id` ASC),
INDEX `fk_jobreq_client1_idx` (`client_id` ASC),
CONSTRAINT `fk_jobreq_service1`
FOREIGN KEY (`service_id`)
REFERENCES `telecom`.`service` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `fk_jobreq_client1`
FOREIGN KEY (`client_id`)
REFERENCES `telecom`.`client` (`id`)
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
SQL script execution finished: statements: 12 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
I am unable to find out why I am getting this error can someone with a high intelligence please assist me.

So it seems all I had to do was to change the name of the table and it went through im guessing somehow the name was magically somehow still in the system

Related

Postgres Update Query

I'm executing below query in postgres 12.11
Table structure as below:
CREATE TABLE pdb_interface.schedule (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
report varchar NOT NULL,
users varchar NOT NULL,
"hour" int4 NULL,
min int4 NULL,
daily_frequency int4 NULL,
daily_workday bool NULL,
weekly_frequency int4 NULL,
weekly_days varchar NULL,
monthly_sected_days varchar NULL,
last_updated timestamp NULL,
deleted bool NULL,
CONSTRAINT schedule_pkey PRIMARY KEY (id),
CONSTRAINT schedule_report_fkey FOREIGN KEY (report) REFERENCES pdb_interface.report("Id"),
CONSTRAINT schedule_users_fkey FOREIGN KEY (users) REFERENCES pdb_interface.users("Id"));
update schedule
set daily_frequency = 12
where id = 'f2f0ba60-f8b1-49ae-a82c-aaddb1a1834b'
The column "id" is of uuid and value is present in the table . However while executing above query, it is giving below error
SQL Error [XX000]: ERROR: no conversion function from character varying to uuid
Is there any work around for the same

PostgreSQL/pgAdmin4 ERROR: there is no unique constraint matching given keys for referenced table "index"

I'm trying to transform my MySQL design to PostgreSQL but when I try to create the table "index":
CREATE TABLE "model1"."index" (
"id_index" INT GENERATED ALWAYS AS IDENTITY ,
"index_name" VARCHAR(5) NOT NULL,
"index_type_id_index_type" INT NOT NULL,
"index_provider_id_index_provider" INT NOT NULL,
"miseq" VARCHAR(45) NOT NULL,
"nextseq" VARCHAR(45) NOT NULL,
PRIMARY KEY ("id_index"),
CONSTRAINT "fk_index_index_type"
FOREIGN KEY ("index_type_id_index_type")
REFERENCES "model1"."index_type" ("id_index_type")
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT "fk_index_index_provider"
FOREIGN KEY ("index_provider_id_index_provider")
REFERENCES "model1"."index_provider" ("id_index_provider")
ON DELETE NO ACTION
ON UPDATE NO ACTION);
I got this error: ERROR: there is no unique constraint matching given keys for referenced table "index".
The two tables containing the foreign keys were created before the "index" table:
CREATE TABLE "model1"."index_type" (
"id_index_type" INT GENERATED ALWAYS AS IDENTITY ,
"name" VARCHAR(3) NOT NULL,
PRIMARY KEY ("id_index_type"));
CREATE TABLE "model1"."index_provider" (
"id_index_provider" INT GENERATED ALWAYS AS IDENTITY ,
"name" VARCHAR(40) NOT NULL,
PRIMARY KEY ("id_index_provider"));
Solved. The error was in another table as #a_horse_with_no_name suggested.

Execute procedure automatically

I have this table
create table preƱadas(
hierro varchar(15) NOT NULL,
hierro_toro varchar (30) NOT NULL,
fecha_esperada_parto timestamp,
observaciones varchar(200),
primary key (hierro),
foreign key (hierro) references animales,
foreign key (hierro_toro) references animales (hierro)
);
i would like to eliminate a record automatically from it when now() is one month past fecha_esperada_parto
Any ideas how to do it?

Error Code: 1822. Failed to add the foreign key constraint. Missing index for constraint 'questions_ibfk_1' in the referenced table 'category'

cannot add foreign key constraint to table
create table users
(
user_id int auto_increment primary key not null,
username varchar(50) unique null ,
email varchar(50) unique ,
passwords varchar(50) not null,
login_status boolean not null
);
create table category (
category_id int primary key not null,
category_name varchar(50) not null
);
create table answers (
id_answer int auto_increment primary key not null,
answer boolean not null
);
create table questions (
question_id int primary key not null,
category_name varchar(50) not null,
content varchar(50) not null ,
foreign key (category_name) references category (category_name)
);
You get this error because there's no index on category_name in the category table. Change that CREATE statement as follows:
create table category (
category_id int primary key not null,
category_name varchar(50) not null,
KEY category_name_index (category_name)
);
From the docs (8.0 version, but the statement is true for older versions):
MySQL requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan. In the referencing table, there must be an index where the foreign key columns are listed as the first columns in the same order. Such an index is created on the referencing table automatically if it does not exist. This index might be silently dropped later, if you create another index that can be used to enforce the foreign key constraint. index_name, if given, is used as described previously.
Also, you're using a varchar(50) as your foreign key, which is not usually a great idea for a variety of reasons. You probably want to use a numeric value, such as category_id, instead.

forward engineer with mysql workbench cannot create the schema

-- MySQL Workbench Forward Engineering
SET #OLD_UNIQUE_CHECKS=##UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET #OLD_FOREIGN_KEY_CHECKS=##FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET #OLD_SQL_MODE=##SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
-- -----------------------------------------------------
-- Schema motivian
-- -----------------------------------------------------
-- -----------------------------------------------------
-- Schema motivian
-- -----------------------------------------------------
CREATE SCHEMA IF NOT EXISTS `motivian` DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci ;
USE `motivian` ;
-- -----------------------------------------------------
-- Table `motivian`.`user`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `motivian`.`user` ;
CREATE TABLE IF NOT EXISTS `motivian`.`user` (
`user_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`username` VARCHAR(100) NOT NULL,
`email` VARCHAR(100) NULL,
`password` VARCHAR(100) NOT NULL,
UNIQUE INDEX `username_UNIQUE` (`username` ASC),
PRIMARY KEY (`user_id`));
-- -----------------------------------------------------
-- Table `motivian`.`calculation`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `motivian`.`calculation` ;
CREATE TABLE IF NOT EXISTS `motivian`.`calculation` (
`category_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`calc` VARCHAR(100) NOT NULL,
`user_id` INT NULL,
`date_created` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`category_id`),
CONSTRAINT `user_id`
FOREIGN KEY ()
REFERENCES `motivian`.`user` ()
ON DELETE NO ACTION
ON UPDATE NO ACTION);
SET SQL_MODE=#OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=#OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=#OLD_UNIQUE_CHECKS;
This is auto generated with mysql workbench but the constraint could not be applied. I tried like this:
CONSTRAINT user_id
FOREIGN KEY (user_id)
REFERENCESmotivian.user(user_id)
ON DELETE NO ACTION
ON UPDATE NO ACTION);
but also don't work.....
This is the error:
Executing SQL script in server
ERROR: Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')
REFERENCES `motivian`.`user` ()
ON DELETE NO ACTION
ON UPDATE NO A' at line 8
SQL Code:
CREATE TABLE IF NOT EXISTS `motivian`.`calculation` (
`category_id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`calc` VARCHAR(100) NOT NULL,
`user_id` INT NULL,
`date_created` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`category_id`),
CONSTRAINT `user_id`
FOREIGN KEY ()
REFERENCES `motivian`.`user` ()
ON DELETE NO ACTION
ON UPDATE NO ACTION)
SQL script execution finished: statements: 8 succeeded, 1 failed
Fetching back view definitions in final form.
Nothing to fetch
Your FK definition is incomplete. Youd did not specify any column. Select your foreign key in the table editor and specify column pairs in the list right to it: