-
Notifications
You must be signed in to change notification settings - Fork 10.7k
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
Troubles with BelongsTo::associate with unsaved Eloquent models #21990
Comments
IMO this is expected behavior. I wouldn't think you could create a relationship between 2 models that are not already represented in the database. I believe at least one of the models must be saved in advance, in order to create the relationship. |
@devcircus but we can access to related model before eloquent push ( |
Yeah i agree with @devcircus here, this looks like intended behaviour |
You can't associate a row from a different table if that row doesn't exist. You don't have an ID (or what ever unique key) to associate until you save it.
|
@jekinney, think about OOP and read my previous comment. At the moment of saving second second model (People) we have ID of first model (City). And one database query is better than 2 anyway (we need to save two models and then make BelongsTo::associate and save again). |
Could we at least add a CantDoThatYetException (or perhaps a TransientOwnerException) so that uses of the associate() method (and perhaps other method) are made of aware of the logic error? |
This is indeed the intended behavior for |
Description:
I was trying to associate Eloquent model (unsaved) model with another (unsaved). After saving in database in table for first model no id of model from another table.
Seems like a bug.
Steps To Reproduce:
And... We have People without City.
The text was updated successfully, but these errors were encountered: