Subject: | Possible bug in updating a foreign key |
Have tried to create a minimal example of what seems like a bug to me.
If I prefetch via a belongs_to relationship, then update the foreign key
on the main object, the related object isn't refreshed. That might be
ok, but when I call discard_changes, it refreshes using the *old* key.
Possibly, I'm doing something I shouldn't be, in which case please let
me know :)
I hope the script is mostly self explanatory. It obviously works against
the attached mysql DB dump.
Thanks,
Sam.
Subject: | dbic_test.pl |
use strict;
use warnings;
package MyApp::Schema::CD;
use base 'DBIx::Class';
__PACKAGE__->load_components(qw/ Core/);
__PACKAGE__->table('cd');
__PACKAGE__->add_columns(qw/cd_id artist_id/);
__PACKAGE__->set_primary_key('cd_id');
__PACKAGE__->belongs_to(
'artist',
'MyApp::Schema::Artist',
{ 'foreign.artist_id' => 'self.artist_id' }
);
package MyApp::Schema::Artist;
use base 'DBIx::Class';
__PACKAGE__->load_components(qw/ Core/);
__PACKAGE__->table('artist');
__PACKAGE__->add_columns(qw/artist_id/);
__PACKAGE__->set_primary_key('artist_id');
package MyApp::Schema;
use base qw/DBIx::Class::Schema/;
__PACKAGE__->load_classes(qw/CD Artist/);
package main;
my $schema = MyApp::Schema->connect(
"dbi:mysql:test-dbic",
"root",
"",
{AutoCommit => 1},
);
my $cd = $schema->resultset('CD')->find(
{
cd_id => 1,
},
{
prefetch => 'artist',
},
);
print "before update: " . $cd->artist->id . "\n";
$cd->artist_id(2);
$cd->update;
$cd->artist->discard_changes;
print "after update: " . $cd->artist->id . "\n";
Subject: | test.sql |
-- MySQL dump 10.11
--
-- Host: localhost Database: test-dbic
-- ------------------------------------------------------
-- Server version 5.0.45-Debian_1ubuntu3.3-log
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;
/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
/*!40103 SET TIME_ZONE='+00:00' */;
/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
--
-- Table structure for table `artist`
--
DROP TABLE IF EXISTS `artist`;
CREATE TABLE `artist` (
`artist_id` int(11) NOT NULL auto_increment,
PRIMARY KEY (`artist_id`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1;
--
-- Dumping data for table `artist`
--
LOCK TABLES `artist` WRITE;
/*!40000 ALTER TABLE `artist` DISABLE KEYS */;
INSERT INTO `artist` VALUES (1),(2);
/*!40000 ALTER TABLE `artist` ENABLE KEYS */;
UNLOCK TABLES;
--
-- Table structure for table `cd`
--
DROP TABLE IF EXISTS `cd`;
CREATE TABLE `cd` (
`cd_id` int(11) NOT NULL auto_increment,
`artist_id` int(11) NOT NULL,
PRIMARY KEY (`cd_id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
--
-- Dumping data for table `cd`
--
LOCK TABLES `cd` WRITE;
/*!40000 ALTER TABLE `cd` DISABLE KEYS */;
INSERT INTO `cd` VALUES (1,1);
/*!40000 ALTER TABLE `cd` ENABLE KEYS */;
UNLOCK TABLES;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
-- Dump completed on 2008-05-17 19:29:54