2

I am facing an issue to assign across cross-databases relationship in laravel migration because my database name contains a dot(.) value, My Database name is = "demo.local.com"

Schema::table('user_details', function(Blueprint $table)
        {
            $table->foreign('user_id')->references('id')
            ->on('`demo.local.com`.users')
            ->onUpdate('RESTRICT')->onDelete('RESTRICT');
        });

I am using that way but its return error like

MariaDB server version for the right syntax to use near '.`com```.`users` (`id`) on delete RESTRICT on update RESTRICT

So it's possible to assign a foreign key when the database name contains a dot(.) value?

Akina
  • 39,301
  • 5
  • 14
  • 25
Mdr Kuchhadiya
  • 431
  • 4
  • 12
  • 1
    Is there a reason you're using dots rather than underscores? – thursday_dan May 12 '21 at 07:39
  • Yes, I have used dots rather than underscores. – Mdr Kuchhadiya May 12 '21 at 10:37
  • I understand that, but why have you chosen dots? The general consensus is to use underscores since it makes life much easier, which would be the quickest solution to this problem unless you are being forced to use dots for some reason? – thursday_dan May 12 '21 at 10:39
  • We already have dots in existing databases. So we need to convert them to underscores if in case it is not working. – Mdr Kuchhadiya May 13 '21 at 04:39
  • Yeah, that's what I would recommend, you can use table names with dots, but as far as I can tell laravel does not support this so you would have to use raw queries. My advice would be to rename your tables to use underscores instead of dots and this will make life much easier. – thursday_dan May 13 '21 at 08:14

1 Answers1

1

I have found the solution by using DB raw query and it's working for me

Schema::table('user_details', function(Blueprint $table)
        {
            $table->foreign('user_id')->references('id')
            ->on(\DB::raw('`demo.local.com`.users'))
            ->onUpdate('RESTRICT')->onDelete('RESTRICT');
        });
Mdr Kuchhadiya
  • 431
  • 4
  • 12