I sent this request to my laravel backend. I get the error SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 1000 bytes (SQL: alter table `users` add unique `users_email_unique`(`email`)) but there is no stack trace or anything. where is this error occuring?
#Why am i not getting the error stack for this error?
21 messages · Page 1 of 1 (latest)
I tried checking the controller function for this request. But there is no sql queries to users table
That error looks like it comes from a migration. What's the controller doing?
Yes, you are right i am running migrations from the controller
protected function createDynamicConnectionAndRunMigrations(
$dbname,
$doaminname,
$name,
$email,
$pass
) {
Log::info("checkpoint 4" );
$host = env("DB_HOST");
$port = env("DB_PORT");
$username = env("DB_USERNAME");
$password = env("DB_PASSWORD");
// Define your dynamic connection parameters
$dynamicConnectionConfig = [
"driver" => "mysql",
"host" => $host,
"port" => $port,
"database" => $dbname,
"username" => $username,
"password" => $password,
// ...other database connection parameters
];
// Set up the new connection configuration dynamically
Config::set(
"database.connections.dynamic_connection",
$dynamicConnectionConfig
);
// Establish the connection
DB::purge("dynamic_connection");
DB::connection("dynamic_connection")->reconnect();
// Run migrations for the dynamic connection
Log::info("checkpoint 5" );
$exitCode = Artisan::call("migrate", [
"--database" => "dynamic_connection",
"--force" => true
]);
if ($exitCode !== 0) {
// Handle the error case
Log::error("Artisan migrate command failed with exit code: " . $exitCode);
}
Log::info("checkpoint 6" );
DB::connection('dynamic_connection')
->table("admins")
->insert([
"name" => $name,
"email" => $email,
"password" => $pass,
"username" => "admin" . rand(),
]);
Log::info("checkpoint 7" );
Artisan::call("db:seed", [
'--class' => LicenceSeeder::class,
"--database" => "dynamic_connection",
]);
only the log "checkpoint 5" gets written into the logs
I think migration call is failing
how do you know its a migration? the error doesn't say anything abt migration
It says it failed to alter a table. That's what migrations do. Are you using an old version of MySQL? The links @noble hatch posted probably have the solution.
Btw you should never use the env() function outside config files. It will always return null when the config is cached (which it should be in production). Also, the connection isn't really dynamic if it uses the default credentials from .env?
oh i thought alter table meant update queries
Nvm, missed the dynamic db-name
Yes its using same username, pass except db name
i will change the env to config
some other dev wrote this code btw
I am wondering what migration file does this command execute?
does it just run "php artisan migrate"?
Yes.
ok got it
Try the suggestions in the Lara casts links. What version of MySQL are you using?