Skip to content
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

Seems to struggle with relationships where the source and target column have the same name #239

Open
WeaponX86 opened this issue Jul 5, 2022 · 2 comments

Comments

@WeaponX86
Copy link

WeaponX86 commented Jul 5, 2022

I have a table users and a table organizations. A user belongs to one organization. When I run I generate the models the relationship from users.org_id to organizations.org_id in the User model is wrong.

CREATE TABLE `users` (
  `user_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `org_id` int(10) unsigned NOT NULL DEFAULT '0',
  `user_name` varchar(150) NOT NULL DEFAULT '',
  `route_state` int(10) unsigned DEFAULT NULL,
  PRIMARY KEY (`user_id`),
  KEY `user_name` (`user_name`),
  KEY `org_id` (`org_id`),
  KEY `users_ibfk_2` (`route_state`),
  CONSTRAINT `users_ibfk_1` FOREIGN KEY (`org_id`) REFERENCES `organizations` (`org_id`),
  CONSTRAINT `users_ibfk_2` FOREIGN KEY (`route_state`) REFERENCES `ppw_share`.`states` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
CREATE TABLE `organizations` (
  `org_id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `org_name` varchar(100) NOT NULL DEFAULT '',
  PRIMARY KEY (`org_id`),
) ENGINE=InnoDB DEFAULT CHARSET=latin1

In User.php the relationship name is the same as the source column name so I can't use it.

	public function org_id()
	{
		return $this->belongsTo(Organization::class, 'org_id');
	}

This works though:

	public function organization()
	{
		return $this->belongsTo(Organization::class, 'org_id');
	}

I cannot change the target column name organizations.org_id. This is an existing DB I inherited.

@finiteinfinity
Copy link
Contributor

Hi @WeaponX86,

Try setting 'relation_name_strategy' => 'foreign_key' in the models.php config file please?

I'm not 100% this will solve your issue, but setting that config value should use the foreign key name (where possible) to generate the relation names instead - which I'm hoping solves your naming issues.

@WeaponX86
Copy link
Author

WeaponX86 commented Jul 14, 2022

I should mention I'm running:

  • PHP v7.3.3
  • Laravel Zero v8.10.0
  • reliese/laravel v1.1.6

I have "relation_name_strategy" set to "foreign_key" already. The only other thing I changed in the config was setting the namespace:
'namespace' => 'App\Models\Shard',

My database contains 346 tables with 501 total foreign keys. All keys are named like {TABLE}_ibfk_{INT}. All of the generated models just get dumped with the column name for the method which is unusable because it conflicts with the actual column name attribute.

I generated the models like this:
php artisan code:models --schema=my_schema

Example code:

		$org = User::firstWhere('user_id', 254)
		->with('org_id')
		->get();
		//Log::debug($org); // Array with one object

		Log::debug('Organization name: '.$org[0]->org_id->org_name); 

Throws this error:
Screenshot 2022-07-14 113229

Here's another example of a table definition and the resulting model:
Screenshot 2022-07-14 114003

Screenshot 2022-07-14 113936

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants