DEFINE-ing an Array of Objects #1443
-
For Schema-full databases, I have a pretty good idea of how to define schemas for a table. Eg. for an object like this: struct MyStruct {
thing: string,
value: number,
} I would call: DEFINE TABLE MyTable SCHEMAFULL;
DEFINE FIELD MyTable.thing ON MyTable TYPE String;
DEFINE FIELD MyTable.value ON MyTable TYPE Number; But, what if I wanted each row in the table to have an array of objects. Eg, a table for a struct like: struct MyOuterStruct {
values: Vec<MyStruct>
} I understand that this is sort of an odd way of using the database. A better idea would be to have one table for MyStruct and another table for MyOuterStruct, and connect them with relations or with an array of references. Still, surrealdb supports this feature – at least in SCHEMALESS mode. Is there a syntax to accomplish the same thing in SCHEMAFULL mode? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
Hey @KeatonTech 👋 I had the same issue when it comes to defining array fields. I could not find any reference in the docs or anywhere else on the internet, only your discussion here. So in the end I basically "solved" (if it is the correct way) it by trial and error. Here is what I figured out so far: All approaches like
This restricts the field So in the end I do not know whether this is the "correct" or intended way of defining it. What I know is that for now this approach works for me. I hope this is of any help to you or anybody else trying to solve the same problem. If anyone knows whether this is the intended way or if there is a "better" way to handle it, please let me know! 🙂 |
Beta Was this translation helpful? Give feedback.
-
I experienced this same kind of experience for defining an array<float, 1536> for a field. Here the solution for my use case: --DEFINE FIELD embedding ON email TYPE array<float, 1536>; -- always empty array []
DEFINE FIELD embedding ON email TYPE array ASSERT (array::len($value) = 1536) || (array::len($value) = 0);
DEFINE FIELD embedding.* ON email TYPE float; Note : |
Beta Was this translation helpful? Give feedback.
-
I ran into this same issue, so I will post my solution with the hope that it helps someone else. The documentation for this solution is found here: https://surrealdb.com/learn/fundamentals/schemafull/define-fields Let's say that you want to model a table for a user's contact info with columns like this:
This is how you would define the table and the fields:
|
Beta Was this translation helpful? Give feedback.
Hey @KeatonTech 👋
I had the same issue when it comes to defining array fields. I could not find any reference in the docs or anywhere else on the internet, only your discussion here. So in the end I basically "solved" (if it is the correct way) it by trial and error. Here is what I figured out so far:
All approaches like
array(string)
(similar to how record links are defined),array<string>
and similar ways do not work. What I did in the end was to first define a field just asarray
and then define all its values viamy_field.*
notation. Example (based on yours):This r…