2012-12-28 11 views
43

Sto tentando di inserire valori nella tabella dei commenti e ricevo un errore. Dice che non posso aggiungere o aggiornare la riga secondaria e non ho idea di cosa significhi.Violazione del vincolo di integrità: 1452 Impossibile aggiungere o aggiornare una riga secondaria:

mio schema simile a questa

-- ---------------------------- 
-- Table structure for `comments` 
-- ---------------------------- 
DROP TABLE IF EXISTS `comments`; 
CREATE TABLE `comments` (
    `id` varchar(36) NOT NULL, 
    `project_id` varchar(36) NOT NULL, 
    `user_id` varchar(36) NOT NULL, 
    `task_id` varchar(36) NOT NULL, 
    `data_type_id` varchar(36) NOT NULL, 
    `data_path` varchar(255) DEFAULT NULL, 
    `message` longtext, 
    `created` datetime DEFAULT NULL, 
    `modified` datetime DEFAULT NULL, 
    PRIMARY KEY (`id`), 
    KEY `fk_comments_users` (`user_id`), 
    KEY `fk_comments_projects1` (`project_id`), 
    KEY `fk_comments_data_types1` (`data_type_id`), 
    CONSTRAINT `fk_comments_data_types1` FOREIGN KEY (`data_type_id`) REFERENCES `data_types` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION, 
    CONSTRAINT `fk_comments_projects1` FOREIGN KEY (`project_id`) REFERENCES `projects` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION, 
    CONSTRAINT `fk_comments_users` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION 
) ENGINE=InnoDB DEFAULT CHARSET=utf32; 

-- ---------------------------- 
-- Records of comments 
-- ---------------------------- 

-- ---------------------------- 
-- Table structure for `projects` 
-- ---------------------------- 
DROP TABLE IF EXISTS `projects`; 
CREATE TABLE `projects` (
    `id` varchar(36) NOT NULL, 
    `user_id` varchar(36) NOT NULL, 
    `title` varchar(45) DEFAULT NULL, 
    `description` longtext, 
    `created` datetime DEFAULT NULL, 
    `modified` datetime DEFAULT NULL, 
    PRIMARY KEY (`id`), 
    KEY `fk_projects_users1` (`user_id`), 
    CONSTRAINT `fk_projects_users1` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION 
) ENGINE=InnoDB DEFAULT CHARSET=utf32; 

-- ---------------------------- 
-- Records of projects 
-- ---------------------------- 
INSERT INTO `projects` VALUES ('50dcbc72-3410-4596-8b71-0e80ae7aaee3', '50dcbc5c-d684-40bf-9715-0becae7aaee3', 'Brand New Project', 'This is a brand new project', '2012-12-27 15:24:02', '2012-12-27 15:24:02'); 

e la dichiarazione mysql che sto cercando di fare simile a questa

INSERT INTO `anthonyl_fbpj`.`comments` (`project_id`, `user_id`, `task_id`, `data_type_id`, `message`, `modified`, `created`, `id`) 
VALUES ('50dc845a-83e4-4db3-8705-5432ae7aaee3', '50dcbc5c-d684-40bf-9715-0becae7aaee3', '1', '50d32e5c-abdc-491a-a0ef-25d84e9f49a8', 'this is a test', '2012-12-27 19:20:46', '2012-12-27 19:20:46', '50dcf3ee-8bf4-4685-aa45-4eb4ae7aaee3') 

l'errore che ottengo sguardi come questo

SQLSTATE [23000]: Violazione del vincolo di integrità: 1452 Impossibile aggiungere o aggiornare una riga secondaria: una lingua straniera il vincolo di chiave non riesce (anthonyl_fbpj. comments, VINCOLO fk_comments_projects1 FOREIGN KEY (project_id) RIFERIMENTI projects (id) ON NO DELETE ACTION ON UPDATE NO ACTION)

risposta

56

Significa semplicemente che il valore per la colonna project_id sul tavolo comments si sta inserendo non lo fa esiste sul tavolo projects. Tenere presente che i valori della colonna sulla tabella comments dipendono dai valori di ID nella tabella Projects.

Il valore 50dc845a-83e4-4db3-8705-5432ae7aaee3 che si sta inserendo per la colonna project_id non esiste nella tabella projects.

+14

Che cosa succede se io ho valore in quel tavolo? Ho lo stesso errore ma ho valori nella tabella e nella corrispondenza dell'ID. –

+0

Ho avuto un problema simile. Eliminare i vecchi dati dalla tabella ha risolto il problema nel mio caso. – Yojan

28

Assicurati di avere project_id nella proprietà fillable del tuo modello Comment.

Ho avuto lo stesso problema, E questa era la ragione.

+4

Lo stesso qui, usando Laravel. Bella presa. – PapaHotelPapa

3

Assicurarsi inoltre che la chiave esterna che si aggiunge sia dello stesso tipo della colonna originale, se la colonna di riferimento non è dello stesso tipo avrà esito negativo.

1

Nel caso in cui qualcuno stia utilizzando Laravel e si stia verificando questo problema. Stavo ottenendo anche questo e il problema era nell'ordine in cui stavo inserendo gli id ​​(cioè le chiavi esterne) nella tabella pivot.

Per essere concreti, di seguito troverete un esempio per una relazione molti a molti:

wordtokens < -> wordtoken_wordchunk < -> wordchunks

// wordtoken_wordchunk table 
Schema::create('wordtoken_wordchunk', function(Blueprint $table) { 
     $table->integer('wordtoken_id')->unsigned(); 
     $table->integer('wordchunk_id')->unsigned(); 

     $table->foreign('wordtoken_id')->references('id')->on('word_tokens')->onDelete('cascade'); 
     $table->foreign('wordchunk_id')->references('id')->on('wordchunks')->onDelete('cascade'); 

     $table->primary(['wordtoken_id', 'wordchunk_id']); 
    }); 

// wordchunks table 
Schema::create('wordchunks', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->timestamps(); 
     $table->string('text'); 
    }); 

// wordtokens table 
Schema::create('word_tokens', function (Blueprint $table) { 
     $table->increments('id'); 
     $table->string('text'); 
}); 

Ora i miei modelli assomigliano segue:

class WordToken extends Model 
{ 
    public function wordchunks() { 
     return $this->belongsToMany('App\Wordchunk'); 
    } 
} 

class Wordchunk extends Model 
{ 

    public function wordTokens() { 
     return $this->belongsToMany('App\WordToken', 'wordtoken_wordchunk', 'wordchunk_id', 'wordtoken_id'); 
    } 
} 

Ho risolto il problema scambiando l'ordine di "wordchunk_id" e "wordtoken_id" nel modello di Wordchunk.

Per il completamento del codice, questo è il modo in persisto i modelli:

private function persistChunks($chunks) { 
    foreach ($chunks as $chunk) { 
     $model = new Wordchunk(); 
     $model->text = implode(' ', array_map(function($token) {return $token->text;}, $chunk)); 
     $tokenIds = array_map(function($token) {return $token->id;}, $chunk); 
     $model->save(); 
     $model->wordTokens()->attach($tokenIds); 
    } 
} 
Problemi correlati