-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
orm: support different foreign key types #19337
Conversation
assert parent.name == new_parent.name | ||
assert parent.children.len == 0 |
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.
imho moving those assertions right after line 197 will be clearer.
The lines in between do not affect the parent
instance.
select from ChildString | ||
}! | ||
|
||
assert children.len == 2 |
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 children survived the death of their parents, nice.
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.
Excellent work.
Fix #18844
This pr adds the ability to have other V types as foreign keys, intead of only ints and improves checker errors.
For example it is now possible to have a primary field of type
string
:At this moment it is hardcoded in the orm code that a sub struct must have a field
id
of typeint
which is used to join the two tables. This pr removes this restriction for arrays and joins the two tables on the primary key, so for the previous example it would evaluate to something like this:Consequently, when a struct with struct arrays is inserted the last insert id is only fetched if the parent struct has a sql serial field:
int [primary; sql: serial]
, else the value of the primary field from the parent struct is passed to the array values.Example to clarify:
🤖 Generated by Copilot at 80499d2
This pull request enhances the ORM module to support custom primary keys of different types and names for structs, and fixes some bugs and errors related to foreign keys and array fields. It also adds new tests and checks for the ORM features and queries in the
vlib/v/tests
andvlib/v/checker/tests
directories.🤖 Generated by Copilot at 80499d2
primary_typ
inorm.v
to store the type of the primary key field of a struct (link, link)primary_typ
if the field has theprimary
attribute inorm.v
(link)pkey_attr_name
inorm.v
to store the name of theprimary
attribute for struct fields (link)has_pkey_attr
andpkey_field
inorm.v
to track whether the struct has a primary key field and what that field is (link, link)pkey_field
instead of a hardcodedid
field in thewhere_expr
of the subquery for array fields inorm.v
(link)primary_field
variable instead of theprimary_field_name
variable to access the name, type, and attributes of the primary key field inc/orm.v
(link, link, link)is_serial
inc/orm.v
to store whether the primary key field has thesql: serial
attribute, which means that the SQL table will generate the primary key value automatically (link)is_serial
inc/orm.v
(link)get_orm_struct_primary_field_name
function toget_orm_struct_primary_field
and?ast.StructField
, respectively, inc/orm.v
(link)ParentString
andChildString
to the test fileorm_sub_array_struct_test.v
to test the case where the primary key field is astring
instead of anint
, and the foreign key field has a different name than the primary key field (link)test_orm_array_different_pkey_type
andtest_orm_relationship_different_pkey_type
to the test fileorm_sub_array_struct_test.v
to test the insert and select queries and the relationship between the structsParentString
andChildString
(link, link)c/orm.v
(link)get_orm_current_table_field
function to be an optionalast.StructField
instead of a defaultast.StructField
, and use thenone
keyword instead of an empty struct field, to indicate the absence of a value, inc/orm.v
(link, link)orm.v
(link)orm_fkey_has_pkey.out
andorm_fkey_has_pkey.vv
to check the error message when a struct has an array field with a foreign key but no primary key (link, link)orm_multiple_pkeys.out
andorm_multiple_pkeys.vv
to check the error message when a struct has more than one primary key field (link, link)member_access_type
inc/orm.v
to store the operator for accessing struct fields, either.
or->
depending on whether the struct is a pointer or not (link, link, link)member_access_type
variable to a later point, after thearrs
variable is declared, inc/orm.v
(link)selected_fields_idx
inc/orm.v
to store the index of the selected fields in the SQL query result, and increment it after each non-array and array field is processed, inc/orm.v
(link, link, link)