I built a PHP web app with the Yii framework, using a postgresql db.
Here are my tables apresentante, veiculo, ferro and lavagem:
CREATE TABLE apresentante
(
id serial NOT NULL,
nome character varying(50) NOT NULL,
morada character varying(50),
cp character varying(15),
nif integer NOT NULL,
telefone integer,
telemovel integer,
codigo integer NOT NULL,
CONSTRAINT apresentante_pkey PRIMARY KEY (id)
)
WITHOUT OIDS;
CREATE TABLE veiculo
(
id serial NOT NULL,
matricula character varying(20) NOT NULL,
classe character varying(10) NOT NULL,
CONSTRAINT id_veiculo PRIMARY KEY (id)
)
WITHOUT OIDS;
CREATE TABLE lavagem
(
id serial NOT NULL,
veiculo_id serial NOT NULL,
data date NOT NULL,
apresentante_id smallint NOT NULL,
ferro_id serial NOT NULL,
apresentante_paga boolean NOT NULL,
CONSTRAINT id_lavagem_pkey PRIMARY KEY (id),
CONSTRAINT id_apresentante_fk FOREIGN KEY (apresentante_id)
REFERENCES apresentante (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT id_ferro_fk FOREIGN KEY (ferro_id)
REFERENCES ferro (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT id_veiculo_fk FOREIGN KEY (veiculo_id)
REFERENCES veiculo (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION
)
WITHOUT OIDS;
CREATE TABLE ferro
(
id serial NOT NULL,
ferro integer NOT NULL,
nome character varying(50) NOT NULL,
CONSTRAINT id_ferro PRIMARY KEY (id)
)
WITHOUT OIDS;
I created the models and forms with Gii.
The problem is that when I try to insert a row into table lavagem it doesn't get the selected FK value, instead it increments the FK values to values that doesn't exist in the FK table.
The exception error:
CDbException
CDbCommand failed to execute the SQL statement: SQLSTATE[23503]: Foreign key violation: 7 ERROR: insert or update on table "lavagem" violates foreign key constraint "id_ferro_fk"
DETAIL: Key (ferro_id)=(4) is not present in table "ferro".. The SQL statement executed was: INSERT INTO "lavagem" ("data", "apresentante_id", "apresentante_paga") VALUES (:yp0, :yp1, :yp2)
Thank you
You've an error in your schema. Foreign keys like:
veiculo_id serial NOT NULL,
Should actually look more like:
veiculo_id int NOT NULL,
It's the id field in the original table that needs to be an auto-incrementing integer; rather than the field that is referencing it.
Related
My code runs in phpmyadmin but the foreign key values are not showing in table and when I update it the message shows "0 rows affected"
my table schema is:
CREATE TABLE fee_report (
fr_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
school_id INT NOT NULL,
branch_id INT NOT NULL,
student_id INT NOT NULL,
ledger VARCHAR(55),
bill_no VARCHAR(255) NOT NULL,
fdate date NOT NULL,
receipt_no VARCHAR(255) NOT NULL,
dr float NOT NULL,
cr float NOT NULL,
balance float NOT NULL,
particulars VARCHAR(155),
updated TIMESTAMP NOT NULL,
CONSTRAINT fk_frschool FOREIGN KEY (school_id)
REFERENCES school(school_id),
CONSTRAINT fk_frbranch FOREIGN KEY (branch_id)
REFERENCES branch(branch_id),
CONSTRAINT fk_frstudent FOREIGN KEY (student_id)
REFERENCES student(student_id)
) ENGINE = INNODB;
INSERT INTO fee_report(school_id,branch_id,student_id,ledger,bill_no,fdate,dr,balance,particulars,feetype)
VALUES(1,1,13,'TGS007','SESNOV1020',CURDATE(),'2850','-14450','11','Tuition Fee,Transport Fee,Smart Class Fee,SA-1 Exam Fee')
Why foreign keys school_id, branch_id and student_id are not showing in table
and when I remove foreign key constraint the values are visible in table
I'm writing a script for an associative table in MySQL and It stops compiling at the second foreign key constraint; does anyone know what could be wrong? Please, I would appreciate it!
create table Adviser(
AdviserID integer not null,
LastName char(25) not null,
FirstName char(25) not null,
AdviserEmail varchar(100) not null,
OfficePhoneNumber char(12) not null,
constraint Adviser_pk primary key(AdviserID),
constraint Adviser_fk foreign key(OfficePhoneNumber)
references Department(OfficePhoneNumber)
on delete no action
on update no action
);
create table Student(
StudentID integer not null,
LastName char(25) not null,
FirstName char(25) not null,
StudentEmail varchar(100) not null,
EnrollmentDate date not null,
GradDate date not null,
Degree char(25) not null,
DormPhoneNumber char(12) not null,
constraint Student_pk primary key(StudentID),
constraint Student_fk foreign key(DormPhoneNumber)
references Dorm(DormPhoneNumber)
on delete no action
on update no action
);
The two tables above work fine, when I make the table below link to the two above something goes wrong with having 2 foreign keys
create table AppointmentDate1(
AdviserID integer not null,
StudentID integer not null,
StudentAppointmentDate date not null,
StudentEndDate date not null,
constraint AppointmentDate1_pk primary key(AdviserID, StudentID),
constraint AppointmentDate1_fk foreign key(AdviserID)
references Adviser(AdviserID)
on delete no action
on update no action,
constraint AppointmentDate1_fk foreign key(StudentID)
references Student(StudentID)
on delete no action
on update no action
);
Can anyone help?
Foreign keys need to have different constraint names. Try this:
create table AppointmentDate1(
AdviserID integer not null,
StudentID integer not null,
StudentAppointmentDate date not null,
StudentEndDate date not null,
constraint AppointmentDate1_pk primary key(AdviserID, StudentID),
constraint fk_AppointmentDate1_AdviserId foreign key(AdviserID)
references Adviser(AdviserID)
on delete no action
on update no action,
constraint fk_AppointmentDate1_StudentId foreign key(StudentID)
references Student(StudentID)
on delete no action
on update no action
);
Just rename two foreign keys, it should work just as below..
I tested it using the following create table script on my local database and I could create AppointmentDate1 table successfully.
create table AppointmentDate1(
AdviserID integer not null,
StudentID integer not null,
StudentAppointmentDate date not null,
StudentEndDate date not null,
constraint AppointmentDate1_pk primary key(AdviserID, StudentID),
constraint AdviserId1_fk foreign key(AdviserID)
references Adviser(AdviserID)
on delete no action
on update no action,
constraint StudentId1_fk foreign key(StudentID)
references Student(StudentID)
on delete no action
on update no action
);
I have 1 user and I want to insert more than one language(and degree) to him. How can I do this with php?
My languagetype table(all languages inserted)
CREATE TABLE TLANGUAGETYPE (
ID INT NOT NULL AUTO_INCREMENT,
language VARCHAR(100) NOT NULL,
PRIMARY KEY (ID)
)
My language table connected with user table.
CREATE TABLE TLANGUAGE (
languageID INT NOT NULL AUTO_INCREMENT,
userID INT NOT NULL,
ID INT NOT NULL,
degree FLOAT,
PRIMARY KEY (languageID),
FOREIGN KEY (userID) REFERENCES TUSER(userID),
FOREIGN KEY (ID) REFERENCES TLANGUAGETYPE(ID)
)
Instead of adding the languageId in user Table. You can create a mapping table which just contains the "USERID" and "LanguageID" columns.
here is my code
CREATE TABLE IF NOT EXISTS items
(
id INT NOT NULL AUTO_INCREMENT,
name varchar(256) ,
description TEXT,
price INT ,
images TEXT,
views INT ,
hidden TEXT,
purchases INT,
PRIMARY KEY (id)
);
CREATE TABLE IF NOT EXISTS methods
(
method_id INT NOT NULL AUTO_INCREMENT,
method varchar(256),
username varchar(256),
password varchar(256),
PRIMARY KEY (method_id)
);
CREATE TABLE IF NOT EXISTS payments
(
payment_id INT NOT NULL AUTO_INCREMENT,
item_id INT NOT NULL,
method varchar(256),
display INT,
PRIMARY KEY (payment_id) ,
FOREIGN KEY (item_id) REFERENCES items (id) ON DELETE CASCADE ON UPDATE CASCADE,
FOREIGN KEY (method) REFERENCES methods (method) ON DELETE CASCADE ON UPDATE CASCADE
)ENGINE=InnoDB DEFAULT CHARSET=latin1;
The first 2 tables gets generated ok, but the third one "payments" gives me error code 150 ! which should be related to FK ?!
any help ?
The primary key in the methods table is method_id not method. And the data type is INT not VARCHAR(256)
You need:
CREATE TABLE IF NOT EXISTS payments
(
payment_id INT NOT NULL AUTO_INCREMENT,
item_id INT NOT NULL,
method_id int, -- this is different
display INT,
PRIMARY KEY (payment_id) ,
FOREIGN KEY (item_id) REFERENCES items (id) ON DELETE CASCADE ON UPDATE CASCADE,
-- and this line is different
FOREIGN KEY (method_id) REFERENCES methods (method_id) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
hey guys, i'm getting this error.
Error 1452 : Cannot add or update a child row: a foreign key constraint fails (`s2794971db/ProfileInterests`, CONSTRAINT `ProfileInterests_ibfk_2` FOREIGN KEY (`InterestID`) REFERENCES `Interests` (`ID`))
I change my tables from myISAM to innodb...found out I needed to so that delete was easier.
I had issues with it so I deleted the table which I needed to create the relationships with.
Then I made it again
I originally had
create table if not exists Users (
ID int not null auto_increment primary key,
FirstName varchar(40) not null,
LastName varchar(40) not null,
UserName varchar(40) not null,
UserEmail varchar(40) not null,
UserDOB timestamp not null,
UserJoin datetime not null
);
create table if not exists Interests(
ID int not null auto_increment primary key,
Interests varchar(40) not null
);
create table if not exists ProfileInterests (
userID int not null References Users(ID),
InterestID int not null References Interests(ID),
MiddleID int not null auto_increment primary key
);
but then I deleted the last table and made it
create table if not exists ProfileInterests (
userID int not null,
InterestID int not null,
MiddleID int not null auto_increment primary key
);
and then I made userID and InterestID into index's and then I added a relation User-> ID for userID and interests->ID for interestID
the error occurs when i'm trying to input data into via a php form.
Any ideas...really need some help!
Obviously, you're trying to insert a record into ProfileInterests for which there is no matching record in the Interests table. Look at the exact insert query, and check that you're supplying a valid value for every field in the table which is part of a foreign key relationship.