Simple join in Aqueduct - dart

I'm newbie in a backend development and stuck with a simple request and ask for help.
I am trying to get a simple join:
select * from product a, product_barcode b
where a.productid = b.productid
Structure of the classes:
Product
class Product extends ManagedObject<product> implements product {}
class product {
#primaryKey
int productid;
String rusname;
String engname;
ManagedSet<Product_barcode> products;
}
Product_barcode
Class Product_barcode extends ManagedObject<product_barcode>
implements product_barcode {}
class product_barcode {
#primaryKey
int productid;
String barcode;
#Relate(#products)
Product product;
}
Request
#Operation.get()
Future<Response> getProducts() async {
final productQuery = Query<Product>(context)..join(set: (a) => a.products);
final res = await productQuery.fetch();
return Response.ok(res);
}
When i make a request I am getting error:
SELECT t0.productid,t0.rusname,t0.engname,t1.productid,t1.barcode,t1.product_productid FROM product t0 LEFT OUTER JOIN product_barcode t1 ON t0.productid=t1.product_productid
column t1.product_productid does not exist
What I am doing wrong? I do not have a column product_productid in my database
Update
Table structure
drugs=# \dt product_barcode
List of relations
Schema | Name | Type | Owner
--------+-----------------+-------+----------
public | product_barcode | table | druguser
(1 row)
drugs=# \d product_barcode
Table "public.product_barcode"
Column | Type | Collation | Nullable | Default
-----------+-------------------+-----------+----------+---------
productid | integer | | not null |
barcode | character varying | | not null |
Indexes:
"product_barcode_pk" PRIMARY KEY, btree (productid, barcode)
drugs=# \dt product
List of relations
Schema | Name | Type | Owner
--------+---------+-------+----------
public | product | table | druguser
(1 row)
drugs=# \d product
Table "public.product"
Column | Type | Collation | Nullable | Default
-------------------------+--------+-----------+----------+---------
productid | bigint | | not null |
rusname | text | | |
engname | text | | |
registrationdate | text | | |
dateofcloseregistration | text | | |
registrationnumber | text | | |
composition | text | | |
zipinfo | text | | |
producttypecode | text | | |
marketstatusid | text | | |
Indexes:
"product_pkey" PRIMARY KEY, btree (productid)

Related

Not null query extremely slow in ajax request partial

I have this simple scope
scope :has_video_link,
-> { where.not(video_link: nil) }
I use it like so in my controller
#videos = Message .featured
.includes(:user, :company, :forum, :topic)
.published
.unremoved
.approved
.has_video_link
.order(created_at: :desc)
.paginate(
page: page,
per_page: limit)
Which I call like so
before_action only: :index
skip_before_action :verify_authenticity_token
def index
self.page_title = 'Home'
fetch_videos
end
That works just fine, and my page loads in under a second on prod no problem. My issue is I have an ajax request that calls this
def videos
fetch_videos
respond_to do |format|
format.js
end
end
On dev this works great but in prod this takes over 60 seconds....
First image is from the first times its called (4ms)
2nd image is the ajax call 69000ms...
Any ideas? How do i fix this?? Thank you!!
edit: here is the explain
EXPLAIN for: SELECT `messages`.* FROM `messages` WHERE `messages`.`is_featured` = 1 AND (messages.created_at<='2021-06-04 12:36:22.241601') AND `messages`.`is_removed` = 0 AND `messages`.`is_approved` = 1 AND (`messages`.`video_link` IS NOT NULL) ORDER BY `messages`.`created_at` DESC LIMIT 5 OFFSET 0
+----+-------------+----------+------------+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------------------------------------------+---------+------+------+----------+------------------------------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+----------+------------+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------------------------------------------+---------+------+------+----------+------------------------------------+
| 1 | SIMPLE | messages | NULL | range | index_messages_on_is_removed_and_user_id_and_created_at,index_messages_on_is_removed_and_message_type_and_created_at,index_messages_on_is_removed_company_id_type_created_at,index_messages_on_is_removed_company_id_created_at_id_user_id,index_messages_on_is_removed_and_created_at,index_messages_on_is_removed_company_id_created_at_rating_count,index_messages_on_is_removed_company_id_user_role_created_at,index_messages_on_is_removed_and_company_id_and_rating_total,index_messages_on_is_removed_and_forum_id_and_created_at,index_messages_on_is_approved,index_messages_on_is_removed_and_is_approved | index_messages_on_is_removed_and_created_at | 7 | NULL | 3470 | 9.0 | Using index condition; Using where |
+----+-------------+----------+------------+-------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------------------------------------------+---------+------+------+----------+------------------------------------+
1 row in set (0.00 sec)
EXPLAIN for: SELECT `users`.* FROM `users` WHERE `users`.`id` = 594568
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
| 1 | SIMPLE | users | NULL | const | PRIMARY | PRIMARY | 4 | const | 1 | 100.0 | NULL |
+----+-------------+-------+------------+-------+---------------+---------+---------+-------+------+----------+-------+
1 row in set (0.00 sec)
EXPLAIN for: SELECT `companies`.* FROM `companies` WHERE `companies`.`id` IN (564013, 562440)
+----+-------------+-----------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+-----------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
| 1 | SIMPLE | companies | NULL | range | PRIMARY | PRIMARY | 4 | NULL | 2 | 100.0 | Using where |
+----+-------------+-----------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
1 row in set (0.01 sec)
EXPLAIN for: SELECT `forums`.* FROM `forums` WHERE `forums`.`id` IN (12224, 7759)
+----+-------------+--------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+--------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
| 1 | SIMPLE | forums | NULL | range | PRIMARY | PRIMARY | 4 | NULL | 2 | 100.0 | Using where |
+----+-------------+--------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
1 row in set (0.00 sec)
EXPLAIN for: SELECT `topics`.* FROM `topics` WHERE `topics`.`id` IN (684474, 684473, 684472, 684470, 684467)
+----+-------------+--------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
+----+-------------+--------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
| 1 | SIMPLE | topics | NULL | range | PRIMARY | PRIMARY | 4 | NULL | 5 | 100.0 | Using where |
+----+-------------+--------+------------+-------+---------------+---------+---------+------+------+----------+-------------+
1 row in set (0.00 sec)
Solved this issue. All I had to do was simply index video_links and that fixed my issue right up!

Symfony 3 & Doctrine - Complex form with OnetoMany and ManyToMany relation

I want to manage the rates of a product in multi-currency and keep historic of currency rates.
So onetomany relation:
A Rates can have many CurrencyRate.
A manyto many relation:
Many currencyRate have many currencies.
RATES :
+-----------------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| rate | double | YES | | NULL | |
| timeStamp | datetime | NO | | NULL | |
| currencyRate_id | int(11) | YES | MUL | NULL | |
+-----------------+----------+------+-----+---------+----------------+
CURRENCY RATES
+-----------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| rate | double | NO | | NULL | |
| timeStamp | datetime | NO | | NULL | |
+-----------+----------+------+-----+---------+----------------+
currencyrateshascurrencies (manytomany join table)
+-----------------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+---------+------+-----+---------+-------+
| currency_id | int(11) | NO | PRI | NULL | |
| currencyRate_id | int(11) | NO | PRI | NULL | |
+-----------------+---------+------+-----+---------+-------+
currencies
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | UNI | NULL | |
| abreviation | varchar(5) | NO | UNI | NULL | |
+-------------+--------------+------+-----+---------+----------------+
I want to generate a form from all of this.
The html form would get all available currencies with a text field to indicate the CurrencyRate.
Ex :
USD <input type="text">
EUR <input type="text">
CNY <input type="text">
...
I saw the documentation on Symfony about manytomany form. But mine is more complex with an additional onetomany relation and text field. I am totaly lost.
Thanks if you can put me on the right direction.
Best regards,
Pierre
I found the response :
First I needed to correct my Schema which didn't need a ManytoMany relation :
Rates :
+-----------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| rate | double | YES | | NULL | |
| timeStamp | datetime | NO | | NULL | |
+-----------+----------+------+-----+---------+----------------+
CURRENCY RATES
+-------------+---------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+---------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| ratio | double | NO | | NULL | |
| currency_id | int(11) | YES | MUL | NULL | |
| Rate_id | int(11) | YES | MUL | NULL | |
+-------------+---------+------+-----+---------+----------------+
Currencies
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | UNI | NULL | |
| abreviation | varchar(5) | NO | UNI | NULL | |
+-------------+--------------+------+-----+---------+----------------+
Then One Form Type
class CurrencyRateType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('currency',EntityType::class, array(
'class'=>'AppBundle:Currencies',
'choice_label' => 'abreviation',
'disabled' => true,
))
->add('ratio', TextType::class);
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => CurrencyRates::class,
));
}
}
which is embedded in the final form type :
class RateType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder ->add('rate')
->add('CurrencyRate', CollectionType::class, array(
'entry_type' => CurrencyRateType::class,
'entry_options' => array('label' => false))
)
->add('save', SubmitType::class, array('label' => 'create'));
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => Rates::class,
));
}
}

Querying in Rails

How can I get the first name and last name from the table User through table Friend which is referenced by the user_id with the condition of user_id 5 and status is pending and place it in a each loop but i also need the contents of friend controller in rails. Thank a lot for the help
Controller
#add = Friend.where(user_id: 5, status: 'Pending')
User Database
+-----------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+--------------+------+-----+---------+----------------+
| id | int | NO | PRI | NULL | auto_increment |
| first_name | text | YES | | NULL | |
| last_name | text | YES | | NULL | |
| email | text | YES | | NULL | |
| contact_number | varchar | YES | | NULL | |
| birth_date | date | YES | | NULL | |
| created_at | datetime | NO | | NULL | |
| updated_at | datetime | NO | | NULL | |
Friend
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| id | int | NO | PRI | NULL | auto_increment |
| request_date | date | YES | | NULL | |
| reason | text | YES | | NULL | |
| status | varchar(255) | YES | | NULL | |
| userid | int | YES | MUL | NULL | |
How about this:
#add = Friend.joins(:user).select("users.first_name, users.last_name").where(user_id: 5, status: 'Pending')
Seems like youre already fetching the id of the user, stored in the user_id field. In order to get the associated user, i would do
#user = User.find(#add.id)
This retrieves the user based on the id you pass, and from there you can do stuff like
#user.first_name
to get the values
All you need to do in Friend model belongs_to user and then
#add =Friend.where("user_id = ? AND status = ?", 5,"Pending")
and
<#add.each do |s|>
<%s.user.first_name%>
<%=<%s.user.last_name%>
// and so on..
<%end%>

Joining two tables, A and B, where the column names of A should be joined with B based on the values of a column in B

The title is a little confusing, so here is an example of the problem I'm facing.
Table:
FORM_QUESTION
Fields:
student_id
q1_ans
q2_ans
q3_ans
q4_ans
q5_ans
x--------------x----------x----------x----------x----------x----------x
| student_id | q1_ans | q2_ans | q3_ans | q4_ans | q5_ans |
x--------------x----------x----------x----------x----------x----------x
| 1 | A | D | B | B | E |
| 2 | D | C | B | A | D |
| 3 | B | C | D | A | B |
x--------------x----------x----------x----------x----------x----------x
The FORM_QUESTION table stores a student's answers for each question.
Here is information on the second table:
Table:
FORM_VALID_ANS
Fields:
question_id
valid_answer
x---------------x----------------x
| question_id | valid_answer |
x---------------x----------------x
| q1_ans | A |
| q1_ans | B |
| q2_ans | A |
| q2_ans | B |
| q2_ans | C |
| q2_ans | D |
| q3_ans | A |
| q4_ans | A |
| q4_ans | B |
| q5_ans | A |
x---------------x----------------x
The second table, FORM_VALID_ANS, stores the valid, acceptable answers for a particular question. So, according to the above table, here is the acceptable list of values for each question:
q1_ans: A, B
q2_ans: A, B, C, D
q3_ans: A
q4_ans: A, B
q5_ans: A
As you can see, the values for the FORM_VALID_ANS.valid_answer include only the question field names for FORM_QUESTION ("q1_ans", "q2_ans", "q3_ans", "q4_ans", and "q5_ans"). I need to check each students' answer to make sure it is a valid value that they can enter, which is specified in the FORM_VALID_ANS.valid_answer field.
The desired output, where XXX represents an invalid value, and all other values would be answered value:
x--------------x----------x----------x----------x----------x----------x
| student_id | q1_ans | q2_ans | q3_ans | q4_ans | q5_ans |
x--------------x----------x----------x----------x----------x----------x
| 1 | A | D | XXX | B | XXX |
| 2 | XXX | C | XXX | A | XXX |
| 3 | B | C | XXX | A | XXX |
x--------------x----------x----------x----------x----------x----------x
Is it possible to join these two tables together and produce these results (or similar results)?

hasMany relationship - deselecting set value from view - does not update DB accordingly

I have seen this behaviour on a few different locations lets say:
class fruits {
static hasMany=[preapples:Apples, apples: Apples, postapples: Apples ]
}
static mapping = {
preapples cascade: 'lock'
apples cascade: 'lock'
postapples cascade: 'lock'
}
static constraints = {
preapples nullable:true
apples nullable:true
postapples nullable:true
}
When the views are generated if on fruits I select many apples apple1 apple2 etc
If I return now and change from apple1 + apple2 to just apple1 - everything is ok
If I deselect all as in if there was only 1 selected and I choose ctrl to unhighlight it - which clearly shows nothing has been selected - when I submit form the value is not actually removed.
Is this something to do with nullable ? or am I missing something obvious
I have added nullable true as suggested which has made no difference from selecting values then unselecting it.
I have also modified actual sql table structure (naming to apples and fruits but before making change / dropping table and after making change having it recreate table - there was no difference to nullable (already nullable)
mysql> describe fruits_apples;
+-----------------------------------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------------------+------------+------+-----+---------+-------+
| fruits_apples_id | bigint(20) | YES | MUL | NULL | |
| apples_id | bigint(20) | YES | MUL | NULL | |
| fruits_postapples_id | bigint(20) | YES | MUL | NULL | |
| fruits_preapples_id | bigint(20) | YES | MUL | NULL | |
|
mysql> describe fruits_shell_scripts;
+-----------------------------------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------------------+------------+------+-----+---------+-------+
| fruits_apples_id | bigint(20) | YES | MUL | NULL | |
| apples_id | bigint(20) | YES | MUL | NULL | |
| fruits_postapples_id | bigint(20) | YES | MUL | NULL | |
| fruits_preapples_id | bigint(20) | YES | MUL | NULL | |
+-----------------------------------+------------+------+-----+---------+-------+
Regards

Resources