Foreign key constraint is incorrectly formed

#1

hi im using relationships and i get this error everywhere
Foreign key constraint is incorrectly formed
well as i should i’m using
.unsigned().references(‘id’).inTable(‘users’)
on many tables but getting error from everywhere

example usage
this.create(‘sub_users’, (table) => {
table.increments()
table.integer(‘user_id’).unsigned().references(‘id’).inTable(‘users’)
table.string(‘username’, 80).notNullable().unique()
table.string(‘password’, 60).notNullable()
table.string(‘name’, 160)
table.integer(‘state’).defaultTo(0)
table.timestamps()
})

0 Likes

#2

so i found the answer myself tnx realy :expressionless:

so problem is in adonis when you run command migrateion:run adonis will not wait for a file to be migrated then do the next one (at least what i saw) so the answer to this article is that you need to migrate file after another in my case i had to cut all related files to another directory and run migration separate times to be fixed . i hope that the creator well make a command that well migrate a file that we select or find’s a way to do it another way . this is not a new issue and it was replyed from the creator in other places

0 Likes

#3

adonis will not wait for a file to be migrated then do the next one

I really don’t think that this is true. See the code linked below. It awaits for every _executeSchema to be completed before continuing.

I already got key constraints working like this:

table.integer('rank_id').unsigned().references('id').inTable('ranks').onDelete('set null')

Are your migration files in the right order (users table first, then sub_users)?

1 Like

#4

yes even after the error in phpmyadmin users table is created bot the migration process stopped after the Foreign key error and sub_user is created but incomplete

the same problem i found someone in giiter ( i may be wrong ) that he had the same problem and he has done the same as me to fix it

ty

0 Likes

#5

Hey! :wave:

Could you create a repository to reproduce this issue?

0 Likes

#6

solved bro it was my projects problem ty

1 Like

#7

Cool!

Maybe you could provide the solution to help other people that may encounter a similar issue. :v:t3:

0 Likes

#8

I had the same problem. My issue was failing to mark the foreign key ID as ‘unsigned’. The foreign key and the primary key being referenced need to be the exact same type.

0 Likes

#9

Same error here.


Edit: Solved removing the .onDelete('set default') method in the chain.
InnoDB does not seem to support ON DELETE SET DEFAULT

0 Likes

#10

Can we report this as an issue? This takes me like a day to find out… And thx a lot for finding this issue.

0 Likes