Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MySQL foreign key referencing a big_increments field #612

Closed
JRubics opened this issue Mar 25, 2022 · 7 comments · Fixed by #615
Closed

MySQL foreign key referencing a big_increments field #612

JRubics opened this issue Mar 25, 2022 · 7 comments · Fixed by #615
Assignees
Labels
bug An existing feature is not working as intended

Comments

@JRubics
Copy link
Contributor

JRubics commented Mar 25, 2022

Hi 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]
@JRubics JRubics added the bug An existing feature is not working as intended label Mar 25, 2022
@josephmancuso
Copy link
Member

You can't have 2 increments on the same table I don't think. Increments is mainly for primary keys

This line:

table.big_increments('user_context_id', length=20).unsigned().nullable()

Should be like:

table.big_integer('user_context_id', length=20).unsigned().nullable()

@JRubics
Copy link
Contributor Author

JRubics commented Mar 28, 2022

Thank you :) I tried your solution but it also doesn't work with the same error. Do you have any other idea what I can try?

@lucadelmonte
Copy link

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.

@josephmancuso
Copy link
Member

hm ok I'll fix this.

@josephmancuso josephmancuso self-assigned this Mar 28, 2022
@JRubics
Copy link
Contributor Author

JRubics commented Mar 28, 2022

Thank you! :)

@josephmancuso
Copy link
Member

This is fixed in latest 2.5.0 version of Masonite ORM

@JRubics
Copy link
Contributor Author

JRubics commented Mar 30, 2022

Thank you! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An existing feature is not working as intended
Projects
Development

Successfully merging a pull request may close this issue.

3 participants