I use PostgreSQL and have a column codes of type text[], and I have another column filterCodes of type string[]. When I query data from the table, I need to check that codes contains at least one element from filterCodes, I try use Intersection and Any but neither seems to work.
How can I do this without writing custom functions?
patientQuery.Where(p => p.Codes.Intersect(filterCodes).Any());
According to documentation Array Type Mapping (look at translation of array1 && array2)
It should be:
patientQuery = patientQuery
.Where(p => p.Codes.Any(c => filterCodes.Contains(c)));
Related
I'm making use of a lot of literal string union types, and arrays of them for some code related to defining SQL tables/views, and all of their columns.
See the example code below where we have a sample user SQL table that has columns: id, username, email, password....
export type UserTableColumnName = 'id' | 'username' | 'email' | 'password';
export type ArrayOfUserTableColumns = UserTableColumnName[]; // This allows for redundant values, but I don't want it to allow them
function choose_some_user_table_columns(chosen_columns: ArrayOfUserTableColumns) {
// function code not important
}
/**
* This is fine, no error should occur:
*/
choose_some_user_table_columns(['id', 'email']);
/**
* I want the code below to trigger a TypeScript typing error due to the 'email' element value being given twice:
*/
choose_some_user_table_columns(['id', 'email', 'email']);
Is there any way to create a type based from (or similar to) UserTableColumnName[] - but where TypeScript will trigger an error if the same value is given more than once? e.g. email being specified twice in the last line of the code sample above.
I'm after a TypeScript solution (rather than a runtime JS check).
And ideally it would also be great if my editor (vscode, or any editor that supports TypeScript) only ever suggests the column names that weren't already in the array. As currently the intellisense will auto-suggest every column regardless of them already being in the array.
You can do this with a mapped type representing each step in a recursive algorithm to generate all permitted array permutations. (TS 4.0+ due to variadic tuple usage, you can do it in older versions, but it gets messy)
type UniqueItems<T extends string, U extends string[] = []> = U | {
[K in T]: UniqueItems<Exclude<T, K>, [...U, K]>
}[T]
However, be aware that this doesn't scale well. With 1 item in the T union, you get 2 tuples. With 2 items, 5 tuples. With N items, 2N + 1 tuples. The answer Fabian linked will be better for some situations, but this will provide significantly better autocomplete for others. Playground link.
I'm using postgresql database which allows having an array datatype, in addition django provides PostgreSQL specific model fields for that.
My question is how can I filter objects based on the last element of the array?
class Example(models.Model):
tags = ArrayField(models.CharField(...))
example = Example.objects.create(tags=['tag1', 'tag2', 'tag3']
example_tag3 = Example.objects.filter(tags__2='tag3')
I want to filter but don't know what is the size of the tags. Is there any dynamic filtering something like:
example_tag3 = Example.objects.filter(tags__last='tag3')
I don't think there is a way to do that without "killing the performance" other than using raw SQL (see this). But you should avoid doing things like this, from the doc:
Tip: Arrays are not sets; searching for specific array elements can be
a sign of database misdesign. Consider using a separate table with a
row for each item that would be an array element. This will be easier
to search, and is likely to scale better for a large number of
elements.
Adding to the above answer and comment, if changing the table structure isn't an option, you may filter your query based on the first element in an array by using field__0:
example_tag3 = Example.objects.filter(tags__0='tag1')
However, I don't see a way to access the last element directly in the documentation.
With doctrine and the query builder I write a SELECT MAX() query, and when this result is passed to another query builder, as a parameter, the query works just fine.
But for some reason, I use native SQL query elsewhere in another Repository, and when I use $repo->getMaxMyThing(), it return me an array of array like
array(array('1' => 42)).
Not that if I want the result, I need to type: $max[0]['1'] (The 0 is a simple array index, but the '1' is a string associated key).
Did any method exists in the Doctrine Bundle to 'convert' it to a simple integer automatically?
When you don't want Doctrine to return entities, you can use the get*ScalarResult() query methods.
Query#getScalarResult(): Retrieves a flat/rectangular result set of scalar values that can contain duplicate data. The pure/mixed distinction does not apply.
Query#getSingleScalarResult(): Retrieves a single scalar value from the result returned by the dbms. If the result contains more than a single scalar value, an exception is thrown. The pure/mixed distinction does not apply.
Here is an example from the documentation:
$query = $em->createQuery('SELECT COUNT(u.id) FROM Entities\User u');
$count = $query->getSingleScalarResult();
$count will be a single value (not an array) as requested.
Can hive array contain custom java objects? i.e. Lets say that there is a custom POJO class called Car.java, can the hive table be defined as follows?
Create table X (arr: Array < Car >)
I know there is a bit of serialization and de-serialization to be done to make this happen, but just wanted to check.
The link just says data_type but not clear on the possible data type.
1) Finding by instance object
Assuming I have the instance object called #topic. I want to retrieve the answers for this given topic. I was thinking I should be able to pass in :topics=>#topic, but i had to do the very ugly query below.
#answers = Answers.where(:topic_ids => {"$in" => [#topic.id]})
2) Getting the string representation of the id. I have a custom function (shown below). But shouldn't this be a very common requirement?
def sid
return id.to_s
end
If your associations are set up correctly, you should be able to do:
#topic.answers
It sounds like the above is what you are looking for. Make sure you have set up your associations correctly. Mongoid is very forgiving when defining associations, so it can seem that they are set up right when there is in fact a problem like mismatched names in references_many and referenced_in.
If there's a good reason why the above doesn't work and you have to use a query, you can use this simple query:
#answers = Answer.where(:topic_ids => #topic.id)
This will match any Answer record whose topic_ids include the supplied ID. The syntax is the same for array fields as for single-value fields like Answer.where(:title => 'Foo'). MongoDB will interpret the query differently depending on whether the field is an array (check if supplied value is in the array) or a single value (check if the supplied value is a match).
Here's a little more info on how MongoDB handles array queries:
http://www.mongodb.org/display/DOCS/Advanced+Queries#AdvancedQueries-ValueinanArray