M
M
mk112021-04-14 21:58:32
Java
mk11, 2021-04-14 21:58:32

Why does the request fail because of the foreign key?

Client database:
60773ad6d70b6290675996.jpeg
Gender database:
60773b0b840b8157818287.jpeg
I am running a query:

INSERT INTO client (FirstName, LastName, Patronymic, Birthday, RegistrationDate, Email, Phone, GenderCode, PhotoPath) VALUES ('111', '111', '111', '1999-10-10', '2021-04-14 09:54:49', '111', '111', 'м', '111')

Error:
Cannot add or update a child row: a foreign key constraint fails (`demotest`.`client`, CONSTRAINT `FK_Client_Gender` FOREIGN KEY (`GenderCode`) REFERENCES `gender` (`Code`) ON DELETE NO ACTION ON UPDATE NO ACTION)

I execute queries like this:
public static ResultSet executeQuery (String sql) {
        ResultSet resultSet = null;
        System.out.println(sql);
        try {
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            if (sql.contains("SELECT")) {
                resultSet = preparedStatement.executeQuery();
            } else {
                preparedStatement.executeUpdate();
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();

        }
        return resultSet;

    }

Why does such an error occur?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2021-04-14
@Rsa97

You don't have an entry in the gender table with Code = 'm'.
Therefore, the write restriction to the associated GenderCode field is triggered.

M
Mercury13, 2021-04-15
@Mercury13

I suspect glitches with encodings. Does it work in prog-admin?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question