#migration problems

23 messages · Page 1 of 1 (latest)

magic tapir
#
//public function up(): void
    {
        Schema::create('comentarios', function (Blueprint $table) {
            $table->id();
            $table->text('contenido');
            $table->timestamp('fechacreacion')->default(now());
            $table->timestamps();
            $table->foreignId('idUsuario')->constrained('usuarios');
            $table->foreignId('idReceta')->constrained('recetas');
        });
    }

  public function up(): void
    {
        Schema::create('usuarios', function (Blueprint $table) {
            $table->id();
            $table->string('nombreCompleto', 255);
            $table->string('apodo', 255);
            $table->string('correo', 255);
            $table->string('contrasena', 255);
            $table->timestamp('fechaCreacion')->default(now());
            $table->tinyInteger('esAdmin')->default(0);
            $table->string('genero', 50)->nullable();
            $table->string('fotoPerfil', 255)->nullable();
            $table->enum('experiencia', ['amateur', 'experimentado', 'chef']);
        });
    }

it keeps telling me that the relation between tables its wrong tho i dont see anything wrong
#

migrations are in separated files*

magic halo
#

Mind explaining what the problem is? Would make it a lot easier to pinpoint where to look at

magic tapir
#
  public function up(): void
    {
        Schema::create('comentarios', function (Blueprint $table) {
            $table->id();
            $table->unsignedBigInteger('idUsuario');            
            $table->unsignedBigInteger('idReceta');
            $table->text('contenido');
            $table->timestamp('fechacreacion')->default(now());
            $table->foreign('idUsuario')->references('id')->on('usarios');
            $table->foreign('idReceta')->references('id')->on('receta');
            $table->timestamps();
        });
    }
#
public function up()
    {
        Schema::create('usuarios', function (Blueprint $table) {
            $table->id();
            $table->string('nombreCompleto', 255);
            $table->string('apodo', 255);
            $table->string('correo', 255);
            $table->string('contrasena', 255);
            $table->timestamp('fechaCreacion')->default(now());
            $table->tinyInteger('esAdmin')->default(0);
            $table->string('genero', 50)->nullable();
            $table->string('fotoPerfil', 255)->nullable();
            $table->enum('experiencia', ['amateur', 'experimentado', 'chef']);
        });
    }
magic halo
#

That's not an explanation, that's just dumping more code

magic tapir
#

it says foreign key are wrong

#

so i needed to send the tables

#

to show them

#

comentarios should have 2 fk, from usuarios and from recetas

magic halo
#

So, what is the actual error..?

magic tapir
#

but when it tries to create the table it says fk are wrong

#
SQLSTATE[HY000]: General error: 1005 Can't create table `foroplatos2`.`comentarios` (errno: 150 "Foreign key constraint is incorrectly formed") (Connection: mysql, SQL: alter table `comentarios` add constraint `comentarios_idusuario_foreign` foreign key (`idUsuario`) references `usarios` (`id`))
#

this one

magic halo
#

Probably because the types don't match, difficult to read tho

magic tapir
#

$table->id(); => $table->unsignedBigInteger('idUsuario');

magic halo
#

And on which Laravel version is this?

magic tapir
magic halo
#

And what's the datatype?

#

In your DB

#

Ok nevermind I see I'm wasting my time here

#

🙄🤦‍♂️