D
D
Damir Shaniyazov2021-01-19 16:23:23
MySQL
Damir Shaniyazov, 2021-01-19 16:23:23

Foreign key constraint is incorrectly formed / I can't create a VC, what's the problem?

I get an error while migrating

Foreign key constraint is incorrectly formed
I'm not strong in the database, tell me what I'm doing wrong.

public function safeUp()
    {
        $this->createTable('profile', [
            'id' => $this->primaryKey(),
            'surname' => $this->string()->notNull(),
            'name' => $this->string()->notNull(),
            'email' => $this->string()->notNull(),
            'gender' => $this->string()->defaultValue("MAN"),
            'birthday' => $this->date(),
            'telephone' => $this->string(),
            'site' => $this->string(),
            'role' => $this->string()->notNull(),
            'company' => $this->string(),
            'about' => $this->text(),
            'city' => $this->string(),
        ]);

        $this->addForeignKey(
            'fk-profile-id-user',
            'profile',
            'id',
            'user',
            'id',
            'NO ACTION',
            'CASCADE'
        );

        $this->addForeignKey(
            'fk-profile-email-user',
            'profile',
            'email',
            'user',
            'email',
            'NO ACTION',
            'CASCADE'
        );
    }


The first foreign key appeared, but there are problems with the second one. The types are identical for user.email and profile.email.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrey Ezhgurov, 2021-01-19
@shaniyazovdamir

Has a unique index been created for the user.email field? The primary key can only point to a field that does not allow duplicate values.
But the situation itself, when two duplicate links are implemented between two tables for different pairs of fields, looks at least strange.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question