I am trying todo a join with LINQ on multiple fields but I keep getting following error:
Error:
The type of one of the expressions in the join clause is incorrect. Type inference failed in the call to 'Join'.
LINQ query:
var query = from s in _dbContext.Samples
join sp in _dbContext.UserSampleTypePurposes on new {SampleTypeId = s.SampleTypeID, PurposeId = s.PurposeID} equals new { SampleTypeId = sp.SampleTypeId, PurposeId = sp.PurposeId}
select s;
SamplteTypeId and and purposeId are foreign keys to the respective tables.
system:
dotnet core 2.2.102
Someone an idea how to fix this? or do it differently?
Related
I have a user who can have meetings from different relationships.He can be directly assigned to a meeting with one too many relationships, he can have user groups with many too many relationships that can have meetings with many too many relationships, and he can be responsible for a meeting with a relationship of one to many.
I want to get all meetings for one user. I don't want to merge the collections because I want to be able to use features like where directly in the database. My attempt is to merge all meeting ids and then get the meetings via 'whereIn'.
$ids = $this->meetings()->select('meetings.id')
->union($this->meetingsFromGroups()->select('meetings.id'))
->union($this->responsibleMeetings()->select('meetings.id'));
return Meeting::whereIn('id', $ids)->get();
When I execute this, I get this error:
"SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'union (select `meetings`.`id` from `meetings` inner join `meeting_user_group` on' at line 1 (SQL:...)"
The SQL that is executed:
select *
from `meetings`
where `id` in (
(
select `meetings`.`id`
from `meetings`
inner join `meeting_user` on `meetings`.`id` = `meeting_user`.`meeting_id`
where `meeting_user`.`user_id` = 1
)
union
(
select `meetings`.`id`
from `meetings`
inner join `meeting_user_group` on `meeting_user_group`.`meeting_id` = `meetings`.`id`
inner join `user_groups` on `user_groups`.`id` = `meeting_user_group`.`user_group_id`
inner join `user_user_group` on `user_user_group`.`user_group_id` = `user_groups`.`id`
where `user_user_group`.`user_id` = 1
)
union
(
select `meetings`.`id`
from `meetings`
where `meetings`.`responsible_id` = 1
and `meetings`.`responsible_id` is not null
)
)
The relations:
public function responsibleMeetings(): HasMany {
return $this->hasMany(Meeting::class, 'responsible_id');
}
public function meetings(): BelongsToMany {
return $this->belongsToMany(Meeting::class)->withTimestamps();
}
public function meetingsFromGroups(): HasManyDeep {
return $this->hasManyDeepFromRelations($this->userGroups(), (new UserGroup())->meetings());
}
I'm using the package Staudenmeir\EloquentHasManyDeep for meetingsFromGroups.
I can't use union on the whole meeting, because then I would get
SQLSTATE[21000]: Cardinality violation: 1222 The used SELECT statements have a different number of columns
because of the pivot fields.
How would the SQL have to change for it to work, and how can I do that with Eloquent?
By default whereIn expects array to perform IN() clause on array items but in your case your are sending a union query which is why it is throwing error.
$ids = $this->meetings()->select('meetings.id')
->union($this->meetingsFromGroups()->select('meetings.id'))
->union($this->responsibleMeetings()->select('meetings.id'))
->pluck('id')->all();
return Meeting::whereIn('id', $ids)->get();
Additionally whereIn accepts a closure method also but not sure how would you call these relateables $this->meetings() ,$this->meetingsFromGroups() & $this->responsibleMeetings() inside closure
return Meeting::whereIn('id', function($query){
$query->select('...')
...;
})->get();
I'm trying to output rows consisting of a value and a list of names. This is my query:
Update person set institution_v2 = (select dv.entity_id
from dictionary_v2 dv
left join dictionary_entry_v2 dev on dev.dictionary_id = dv.id
left join person p on p.name = dev.entry_value
JOIN journal_person_relation jpr on jpr.person_id = p.person_id
JOIN journal j on jpr.journal_id = j.journal_id)
But it fails with:
SQL Error [21000]: ERROR: more than one row returned by a subquery used as an expression
how can i solve this problem?
Presumably, you intend a correlated subquery. So, don't re-use person in the subuqery:
update person p
set institution_v2 = (select dv.entity_id
from dictionary_v2 dv join
dictionary_entry_v2 dev
on dev.dictionary_id = dv.id
where p.name = dev.entry_value
);
Note: This could still return duplicates. It is possible that a single value is not appropriate for the column -- perhaps you want an array -- or if an arbitrary matching value works, use limit 1.
I don't think the journal tables are adding anything to the logic.
I am trying to update node properties of a PostgreSQL database that I have running in a virtual environment. I am building a Jazz knowledge base and would like to use the node ids to query additional properties from the PSQL database using APOC.
I have tried unwinding the ids and using them to run the SQL statements with APOC.
This is the code I have tried thus far:
MATCH (a:Artist)
WHERE a.genre = 'jazz'
WITH COLLECT(a.mbid) AS ids
UNWIND ids AS id
CALL apoc.load.jdbc('myDB',
"select DISTINCT a.gid, ar.name as country FROM artist a INNER JOIN area ar on a.area = ar.id WHERE a.gid = ?", [id]) YIELD row
MATCH (a:Artist) WHERE a.mbid = row.gid SET a.country = row.country
RETURN COUNT(a.country)
I am running into an error saying "ERROR: operator does not exist: uuid = character varying". Is there anyway to use the ids from the Cypher query to update each node individually through individual SQL statements with APOC?
Assuming the gid column is a UUID type in your Postgres database and that the error is when executing the query via apoc.load.jdbc, the following explicit casts of your IDs in the SQL should at least have the comparison work and have the query return a string value you can compare on:
MATCH (a:Artist)
WHERE a.genre = 'jazz'
WITH COLLECT(a.mbid) AS ids
UNWIND ids AS id
CALL apoc.load.jdbc('myDB',
"select DISTINCT a.gid::text, ar.name as country FROM artist a INNER JOIN area ar on a.area = ar.id WHERE a.gid = UUID(?)", [id]) YIELD row
MATCH (a:Artist) WHERE a.mbid = row.gid SET a.country = row.country
RETURN COUNT(a.country)
I am trying to run a select statement from two tables, the data that I want to return takes on 3-4 joins to achieve. I am getting the error
ORA-00904: "ADDRESS_USAGES"."AUS_PRO_REFNO": invalid identifier
when both tables and columns exist. I have read the post relating to this error but given that I am just starting out I cant make head nor tail of them. Any suggestions (be gentle). SQL below TIA
select ins_srq_no, adr_line_all from inspections
join properties
on inspections.ins_pro_refno = properties.pro_propref
join addresses
on properties.pro_propref = address_usages.aus_pro_refno
join address_usages
on address_usages.aus_pro_refno = addresses.adr_refno
where fsc.address_usages.end_date is null;
This on clause is incorrect, because you haven't yet joined to the address_usages table: on properties.pro_propref = address_usages.aus_pro_refno. This is causing Oracle to throw the error you're seeing; it's not about the table or column not existing, it's the fact that within that query, that identifier is invalid because you haven't yet joined to the table.
At a guess (I can't be 100% sure without seeing your table structures and foreign keys), you need to join addresses back to properties. If so, the query should look something like this:
select ins_srq_no, adr_line_all
from inspections
inner join properties
on inspections.ins_pro_refno = properties.pro_propref
inner join addresses
on properties.pro_propref = addresses.[column name of FK to properties]
inner join address_usages
on address_usages.aus_pro_refno = addresses.adr_refno
where fsc.address_usages.end_date is null;
I'm getting the error:
the multi-part identifier "IC.industry" could not be bound
when making this SQL query from a JSP page via JDBC:
select C.company, C.shname, C.fullname, count(d_to_c.designer)
from companies C
left join ind_to_c IC on C.company = IC.company
left join d_to_c on C.company= d_to_c.company
where IC.industry = ?
group by C.company, C.shname, C.fullname
order by C.shname
and I'm trying to run it as a prepared statement, where I'm setting the parameter via (for example) stmt.setObject(1, 7) prior to running stmt.executeQuery().
Now, what's weird is: If I execute this with the ? and set the parameter as I just mentioned, I get the "could not be bound" error. If, however, I just change the query and hardcode the number 7 into the text of the query, it works!
So it has something to do with binding that parameter.
But I can't seem to figure out what.
Anybody?
UPDATE: Per request, the table definition for ind_to_c:
industry - int(11)
company - int(11)
(it's just a table that defines the m2m relationship between industries and companies)
UPDATE 2: Also per request, the full JSP code. I had to pull this out of a call to an abstraction of the database connection (which we use to store prepared statements, etc.
// conn has been initialized as the db connection object.
int parent_id = 7;
PreparedStatement ps = conn.prepareStatement("select C.company, C.shname, C.fullname, count(d_to_c.designer) from companies C left join ind_to_c IC on C.company = IC.company left join d_to_c on C.company = d_to_c.company where IC.industry = ? group by C.company, C.shname, C.fullname order by C.shname");
ps.setObject(1, parent_id);
ResultSet rs = null;
rs = ps.executeQuery();
What's the data type for industry? Does it make a difference if you use the type specific bind methods like stmt.setInt(1,7) instead?
edit: also, not related to the question, but you should probably remove C.cid from the SELECT. Some variants of T-SQL will infer that you want to group by that column since it is not the subject of an aggregation function, even though you don't specifiy it in the GROUP BY clause.
Back on topic, can you post the table definition for ind_2_c? The nature of the error would seem to indicate that it has no column called industry.
Have you tried passing a named parameter (i.e: #industry) instead of a question mark?
Maybe I am just thinking to simple here because i do not know JSP to well but would dit not just work:
int parent_id = 7;
PreparedStatement ps = conn.prepareStatement("select C.company, C.shname, C.fullname, count(d_to_c.designer) from companies C left join ind_to_c IC on C.company = IC.company left join d_to_c on C.company = d_to_c.company where IC.industry = ? group by C.company, C.shname, C.fullname order by C.shname");
ps.setInt(1, parent_id );
ResultSet rs = null;
rs = ps.executeQuery();
I see from your comments that you can only use SetObject.
But why do you pass an object array instead of a single object? (if I read Java correctly)
I upgraded to the newer v. 2.0 of the MS-SQL JDBC driver, and magically it worked.