-
-
Notifications
You must be signed in to change notification settings - Fork 824
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
Update DAO to use declared primary field, do not assume... #24275
Conversation
(Standard links)
|
1a96ddf
to
615f874
Compare
*/ | ||
protected function getPrimaryKey(): array { | ||
return static::$_primaryKey; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There seems to be a tension in that:
- SQL tables can have composite primary keys
- Civi SQL tables (DAOs) never (almost never?) have composite primary keys. (At least, I can't think of any off the top of my head...)
I feel like the question is whether the system permits composite PKs:
- If composite PKs are allowed, then
getPrimaryKey(): string[]
(as above) is good, but the other references to[0]
invite quiet bugs. - If composite PKs are not allowed, then
getPrimaryKey(): string
would be better. All the other[0]
bits can be cleaned out, and there's no risk of them being mistaken.- (Probably
getPrimaryKey()
should embed a validation -- ie complain if there is composite PK.)
- (Probably
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah - we do permit composite primaryKeys
- I have an extenstion that does that - but they won't be working here before or after & if feels like a big risk of scope blow out to fix a hypothetical problem (hypothetical that they would ever expect this code path to work for them) if we worry about that - hence I just commented to at least give a clue for someone who might care in the future
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(the main support we provide for composite primaryKeys at the moment is in GenCode generating the sql)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's how APIv4 handles it:
- At the lowest level, APIv4 admits the possibility composite keys and
$entity[primary_key]
is passed around as an array. - But the helper function
CoreUtil::getIdFieldName()
returns a string (the first item in the array), because that's all the API really cares about.
Since this is a low-level function, I guess returning the array is ok. At least it leaves the door open to improve support for composite keys sometime in the future.
I recommend renaming the function getPrimaryKeys
for clarity.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm OK with getPrimaryKeys
- I don't prefer it, because the property is primaryKey
- but I'm OK to rename it if it is mergeable with that
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok you're right let's stick with matching the property name.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with wanting to punt on how to fix composite-PKs. NB I think it's clear that composite-PKs will not behave well, and (to a first skim/guess) they probably behave badly (eg overwriting unrelated records).
The best way to punt on an unsupported scenario is throw an error...
I've tacked a commit on #24287 to make this safer.
@totten I hit merge before your commit. Will need to open a new PR |
Overview
Update DAO to use declared primary field, do not assume
Before
The
_primaryKey
for a table can be declared in the spec for an entity - but when doing an update theDAO
assumesid
rather than checking itAfter
When doing an update the
_primaryKey
declared is looked for & used, rather than assumingid
Technical Details
the use of
_id
in the import tables is a bit annoying but there is a reason - ie most of the column_names in the table are derived from the import source (e.g the csv). It could maybe be reconfigured with some faffing around - but this change is required to make already-present code workComments