MySQL foreign key referencing a big_increments field
See original GitHub issueHi all! I am trying to create a foreign key referencing a big_increments field, but I get (1215, 'Cannot add foreign key constraint') error. I think it is because I can’t create a field that is the same type as big_increments (unsigned non nullable auto incrementing big integer with length of 20) because when I tried creating big integer with unsigned() modifier, its length got shortened to 10.
Example of my code:
class CreateUsersTable(Migration):
def up(self):
"""
Run the migrations.
"""
with self.schema.create('users') as table:
table.big_increments('id')
...
class CreateAppsTable(Migration):
def up(self):
"""
Run the migrations.
"""
with self.schema.create('apps') as table:
table.big_increments('id')
table.big_increments('user_context_id', length=20).unsigned().nullable()
...
table.index('user_context_id', name=apps_user_context_id_index')
table.foreign('user_context_id', name='apps_user_context_id_foreign') \
.references('id').on('users') \
.on_delete('cascade')
Can you please check if it is possible to create foreign key referencing a big_increments field?
Desktop (please complete the following information):
- OS: Linux
- Version Ubuntu 21.10
What database are you using?
- Type: [MySQL]
- Version [14.14 Distrib 5.7.30, for Linux (x86_64) using EditLine wrapper]
- Masonite ORM [2.4.2]
Issue Analytics
- State:
- Created a year ago
- Reactions:5
- Comments:7 (6 by maintainers)
Top Results From Across the Web
Setting a foreign key bigInteger to bigIncrements in Laravel 5.4
I'm confused as to how because I'm matching the column types. Here is the other table. public function up() { Schema::create('social_logins', ...
Read more >13.1.17.5 FOREIGN KEY Constraints - MySQL :: Developer Zone
A foreign key relationship involves a parent table that holds the initial column values, and a child table with column values that reference...
Read more >Week 13: increments/bigIncrements "Cannot add foreign key ...
In my book table migration, the id was created like this: $table->bigIncrements('id');. This generates a column with the MySQL data type of bigint...
Read more >Referencing column is incompatible when adding foreign keys
The id column on the tables is set using bigIncrements('id'). The column housing the foreign keys are of type bigInteger(column_name).
Read more >Be Careful: Laravel 5.8 Added bigIncrements As Defaults
Either change original migration from bigIncrements() to just increments(); · Or in your foreign key column do bigInteger() instead of integer().
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Hello, i have the same error reported on the issue, the problem seems to be that using
table.big_integer('user_context_id', length=20).unsigned().nullable()when calling unsigned(), the type of the fields gets changed to INT, from BIGINT, i think it is a bug here https://github.com/MasoniteFramework/orm/blob/2.0/src/masoniteorm/schema/Blueprint.py#L596, after calling usnigned the type of the column is changed to unsigned_integer , which here https://github.com/MasoniteFramework/orm/blob/2.0/src/masoniteorm/schema/platforms/MySQLPlatform.py#L40 is defined as “INT UNSIGNED” in mysql.Thank you! 🎉