-
Notifications
You must be signed in to change notification settings - Fork 442
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Foreign key not set when using create_association magic method? #546
Comments
structure should be like this CREATE TABLE `jobs` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `exports` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`job_id` int(11) DEFAULT NULL,
`url` VARCHAR(1000) NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB DEFAULT CHARSET=utf8; If there is no condition of links quite simply write but in your case should be a link $has_many or $has_one then when such entries $job->create_export([
"url" => "http://www.example.org"
]); whereas when such record create_export automatic fill field job_id and yes I also have a question to the developers about filling if a many-to-many through the staging table in version 1.1 many have not tried it was impossible to fill all 3 tables We have to first save the news ....
$news->save(); // Store news
$tag = Tag::create(['name' => $tag]); // Store tags
$tag->create_news_tags([ 'news_id' => $news->id]); // Store the relationship between news and tags |
This is probably because you create the export before the job has been saved. Try calling |
Thanks for your quick replies, @visavi and @koenpunt
I've tried changing the code to this: $job = new Job(["name" => "testJob"]);
$job->save();
$job->create_export([
"url" => "http://www.example.org"
]);
$job->save();
$job->reload(); And to this: $job = new Job(["name" => "testJob"]);
$job->save();
$job->reload();
$job->create_export([
"url" => "http://www.example.org"
]);
$job->save();
$job->reload(); But in both cases I'd like to keep the foreign key on the Jobs table if possible. However, I tried the suggestion of @visavi and updated the MySQL schema such that the foreign key was on the Export table and was named
Which seems to indicate that belongs_to is correctly looking for the foreign key attribute on the Jobs? |
To add to @visavi's answer, when going with the schema he suggests, you'll have to update the |
I'd really like to keep the relationship as a "belongs to" (i.e., the foreign key on the Job table) if at all possible. Interestingly, when I run this code: $job = new Job(["name" => "testJob"]);
$job->export = Export::create([
"url" => "http://www.example.org"
]); I get the following exception:
But when I run: $job = new Job(["name" => "testJob"]);
var_dump($job->export); Then So it seems there is an inconsistency: I can read from the Have I configured something incorrectly when setting up my belongs to association or DB schema? |
Just so I can be a little more sure that it's perhaps not my setup... I've been able to recreate this issue with the following new PHP AR unit test, which I've added to Relationship.php: public function test_belongs_to_create_association_sets_foreign_key()
{
$event = $this->get_relationship();
$values = array('city' => 'Richmond', 'state' => 'VA', 'name' => 'Club 54', 'address' => '123 street');
$venue = $event->create_venue($values);
// Adding the following lines seems to have no effect
// $event->save();
// $event->reload();
$this->assert_equals($venue->id, $event->venue_id);
} The test fails with the following output:
Which seems to imply that the event's foreign key column (venue_id) has not been set to the ID of the new venue (10). Looking at the fixtures, it seems that this event (ID 5) has venue_id 6 by default. |
Ok, I think I might have a solution for this... I've cooked up PR #548. |
I have the following (MySQL) database schema:
With the following PHP ActiveRecord models:
When I run the following code, I expected
export_id
to be the same asExport::last()->id
but it isnull
:Interestingly,
$job->name
has the correct value (testJob
). The MySQL database has the following data:I've tried the code on both v1.1.2 and the master branch, and am getting the same results on both versions.
Any hints as to what I might be doing wrong? Could this be a bug?
The text was updated successfully, but these errors were encountered: