pBlueG / SA-MP-MySQL

MySQL plugin for San Andreas Multiplayer
BSD 3-Clause "New" or "Revised" License
196 stars 80 forks source link

Is there a way to use ORM vars with null values? #188

Open 9danst opened 6 years ago

9danst commented 6 years ago

ex:

// orm creation/etc omitted
orm_addvar_int(ormid, data[playerid][P_FACTION_ID], "faction_id"); // <- Foreign key,  accepts NULL (no faction)

Register(playerid)
{
  //  what should i use to set it NULL ?
  data[playerid][P_FACTION_ID] = 0; //  0 != NULL. It's not an option in factions (there's not a faction=0 row) and of course it produces:
  orm_insert(ormid);  //  Error: Cannot add or update a child row: a foreign key constraint fails
}
earltacs commented 6 years ago

orm can be set to null though but the thing is a foreign can't become null since it should never be null and should carry a value of what its foreign of... i.e a userid from a character details in the database is a foreign key to the primary ket of the userid of the accounts details in the database... You can null a non key variable...

Zelyutin commented 6 years ago

@earltacs what do you mean by "should never be null"? Anyway, joining the problem of the issue, I have following scenario: h_number and h_owner_ID in houses table in database, first one is just the int, second one is nullable int field that is foreign key also. I'm trying to assign ORM to this. For example, I have the house with number 3 and no owner, so it's null in DB. After orm_apply_cache, changing the number, I want to save changes with orm_save() but I simply can't do that cause owner_ID became 0 in pawn, so we've got this:

error #1452 while executing query "UPDATE houses SET h_number='5',h_owner_ID='0' WHERE h_ID='1' LIMIT 1": Cannot add or update a child row: a foreign key constraint fails

Does it mean that it's not able to work with not nulls(as it's in pawno) at all and I must change my columns in database, disabling Foreign Keys and set "no-owner values" to simple int zero?

maddinat0r commented 6 years ago

The ORM system doesn't support the NULL value yet, that's why I assigned this issue to me. I currently don't have the time to work on this, but I'll look into this problem in detail when I'm free.

Zelyutin commented 6 years ago

@maddinat0r Thank you! Will be very glad to see this update in the future!

AGraber commented 5 years ago

The only reason I'm not using ORM is because NULL doesn't work. pls fix!

kazon-m commented 5 years ago

It is impossible to use ORM, in that NULL does not work. please fix!

Zekiloni commented 6 months ago

After long time, please fix, joke, at least we need method where we can check is column (in this scenario FK ) mapper to var, basically orm_does_var_exist, if does if u need to map FK with good FK value orm_addvar_int, otherwise if u want to remove orm_delvar