When im trying to sync or attach in server side isn't working, here my models:
Migration Cuenta:
public function up()
{
Schema::create('cuentas', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->id();
$table->string('accountName');
$table->integer('potencialP');
$table->integer('potencialR');
$table->timestamps();
});
}
Cuenta Model:
class Cuenta extends Model
{
use HasFactory;
protected $fillable = ['id','accountName','potencialP','potencialR'];
public function options(){
return $this->belongsToMany('App\Models\Option','cuentas_options');
}
}
Migration Option:
public function up() {
Schema::create('options', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->id();
$table->string('titulo');
$table->integer('puntos');
$table->unsignedBigInteger('field_id');
$table->timestamps();
$table->foreign('field_id')->references('id')->on('fields');
});
}
Option Model:
class Option extends Model
{
use HasFactory;
protected $fillable = ['id'];
public function cuentas(){
return $this->belongsToMany('App\Models\Cuenta','cuentas_options');
}
}
Cuentas_option MIGRATION:
public function up()
{
Schema::create('cuentas_options', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->id();
$table->string('cuenta_id');
$table->unsignedBigInteger('option_id');
// $table->timestamps();
$table->foreign('cuenta_id')->references('id')->on('cuentas');
$table->foreign('option_id')->references('id')->on('options');
});
}
MyExampleCode:
$cuenta = Cuenta::find('8963596291');
$option = Option::all()->pluck('id');
$cuenta->options()->sync($option);
return $cuenta;
It returns:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (`potencial_pr`.`cuentas_options`, CONSTRAINT `cuentas_options_cuenta_id_foreign` FOREIGN KEY (`cuenta_id`) REFERENCES `cuentas` (`id`)) (SQL: insert into `cuentas_options` (`cuenta_id`, `option_id`) values (8963596291, 1))
In my local server is working properly but not in server hosting, can someone help me?
EDIT: IM USING LARAVEL 8, AND UPDATED THE CUENTAS_OPTIONS MIGRATIONS:
public function up()
{
Schema::create('cuentas_options', function (Blueprint $table) {
$table->engine = 'InnoDB';
$table->id();
$table->unsignedBigInteger('cuenta_id');
$table->unsignedBigInteger('option_id');
// $table->timestamps();
$table->foreign('cuenta_id')->references('id')->on('cuentas');
$table->foreign('option_id')->references('id')->on('options');
});
}
THE PROBLEM CONTINUE, IM USING 10.4.11-MariaDB - mariadb.org binary distribution on local host and 5.6.49-cll-lve - MySQL Community Server (GPL) on server, could be this the problem?
I solved it by adding the KEYTYPE on the CUENTAS model and AUTO_INCREMENT=FALSE