thedevdojo / voyager

Voyager - The Missing Laravel Admin
https://voyager.devdojo.com
MIT License
11.78k stars 2.67k forks source link

BREAD on table where foreign key is a string (belongTo relationship) #5158

Closed MikadoInfo closed 3 years ago

MikadoInfo commented 3 years ago

Version information

Description:

I have two tables products and prices related with a one to one relationship, the primary key of the products table is a string type named id (that represent the sku of a product), in the prices table the foreign key is named product_id.

When i try to add a new price using the price's BREAD I get this herror: SQLSTATE[HY000]: General error: 1364 Field 'product_id' doesn't have a default value

The problem is that I need to asign a price to an existing product. A price without a product can't exist. But this error is telling me to make the product_id in the prices table nullable or assign a default value. Obviously i can't assign a default value because the price has to be related to an existing product and if I change the product_id to nullable I have to set the price column nullable too. If I do so I'm able to set a price but the BREAD adds an extra empty record in the database. And in any case it is not the correct solution

Additional context

Before changing datatype from bigint ro string everything worked normally.

Screenshots

Prices BREAD with belongsTo relationship settings:

Modifica BREAD per la tabella prices

Full error message:

messaggio_errore

Tables involved:

schema

Result if I set product_id and price to nullable: 2 records are inserted one is empty

prod_id e price null

MikadoInfo commented 3 years ago

Solved I made a mistake in my model

github-actions[bot] commented 3 years ago

This issue has been automatically locked since there has not been any recent activity after it was closed. If you have further questions please ask in our Slack group.