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

Pivot Tables #7

Open
MACscr opened this issue Nov 29, 2021 · 2 comments
Open

Pivot Tables #7

MACscr opened this issue Nov 29, 2021 · 2 comments

Comments

@MACscr
Copy link

MACscr commented Nov 29, 2021

Should i just create vanilla laravel migration tables for pivot tables or is there a way to handle it with this library? Lets say I have a Post model and a Category model and I want to be able to associate multiple Categories to a Post (belongsToMany).

@MACscr
Copy link
Author

MACscr commented Dec 13, 2021

hmm, doesnt seem to handle foreign keys well. I ended up having a custom pivot table model for one of mine using:

    public function migration(Blueprint $table)
    {
        $table->unsignedBigInteger('bid_id')->index();
        $table->foreign('bid_id')->references('id')->on('bids')->onDelete('cascade');
        $table->unsignedBigInteger('user_id')->index();
        $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
        $table->primary(['bid_id', 'user_id']);
        $table->timestamps();
   }

and that ended working the first time, but if you run migrate:auto again when you make other new models/migrations, i ended up getting the following error:

SQLSTATE[23000]: Integrity constraint violation: 1022 Can't write; duplicate key in table '#sql-48d_8e8' (SQL: alter table `table_bid_bookmarks` add constraint `table_bid_bookmarks_bid_id_foreign` foreign key (`bid_id`) references `bids` (`id`) on delete cascade)

Here is a decent package for helping with creating some pivot table migration files though:
https://github.com/laracasts/Laravel-5-Generators-Extended

@heyjoe1984
Copy link

yeah its cause of the way it creates the indexes when creating the temporary table to diff with

best bet is to use traditional migration files for complex pivots like this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants