How use schemas of SQL Server with tds_ecto? - ecto

I have a model like this
defmodule AppRest.Category do
use AppRest.Web , :model
schema "ventas.categories" do
field :name, :string timestamps()
end
end
when i use it in the controller, with Repo.all
def index(conn, _params) do
categories = Repo.all(Category)
render(conn, "index.json", categories: categories)
end
it generate the query like this
SELECT g0.[id], g0.[name], g0.[inserted_at], g0.[updated_at]
FROM [global.categories] AS g0
then SQL Server can't process the query and return this error:
The object name 'Ventas.categories' is not valid.
The query in right format is like this
SELECT g0.[id], g0.[name], g0.[inserted_at], g0.[updated_at]
FROM [global].[categories] AS g0`
So the question is how i can set the name of schema of the table of SQL Server, in the model?
I hope any can help me with this.

Related

How to pass WHERE clause to Sequelize generated subqueries?

I have a Sequelize view that uses joins like this:
select * from tableA left join (select * from tableB where someKey={some input}) newTableB on tableA.someId = newTableB.someId
Now, when I create a view from this query, I can create a sequelize model for it. But, I want to execute the query in sequelize... like -
models.tableView.findAll({ some clause })
In the above where clause, I want to pass the value of someKey (referring to someKey={some input} in the sample view). How do I do that?
I don't want to fetch all data and then apply the tableB.someKey=something (as that will take far more time)

Fetch jsonb column of postgres db

I am using node-postgres to select and insert data into postgres. I have some column of jsonb type which I am fetching from db by using below query
getEmployee() {
return SELECT empId, empData FROM employee WHERE empId = $1;
}
where empData is jsonb type of column. Below is code snippet which use above query.
const employee = await DBService.query(pgObj.getEmployee(), [empId]);
when I am trying to get empData from employee I am getting empty value.
const { empData } = employee;
I am not sure what I am missing here. Is this the correct way to fetch josnb column of postgreas db in nodejs?
Are you sure empdata is even populated, in the database? Maybe it's empty.
Also, what are the jsonb fields of empdata?
To get the actual sub-fields of empdata, you need the ->> operator. eg:
get the whole json object as text
SELECT empId, empData::text
FROM employee where empId = $1
get individual attributes
SELECT empId, empData->>annual_pay as salary
FROM employee WHERE empId = $1;
etc...
You can also try
Have a look here: https://kb.objectrocket.com/postgresql/how-to-query-a-postgres-jsonb-column-1433
I haven't tried these out, I'm not in front of postgres right now.

How to get related field value from database in odoo 11 and postgresql?

I am trying to get a related field value from database, but it showing column 'column_name' does not exist.
When i try to find out the value of product_id or using join to find the common data between sale.order and product.product Model . but it showing column 'column_name' does not exist.
In sale.order model the field defination is like
product_id = fields.Many2one('product.product', related='order_line.product_id', string='Product')
But when i try to join two table like below code to fetch all data as per product, like below code.
select coalesce(p.name,'Unassigned Product'), count(*) from sale_order o left join product_product p on o.product_id = p.id where o.state = 'sale' group by p.name;
It showing below error,
column o.product_id does not exist
LINE 1: ... from sale_order o left join product_product p on o.product_...
When i try to get data from sale_order table like below code.
select product_id from sale_order;
It showing below error.
column "product_id" does not exist
Can any one help me to get that value.
To access a related field from database , you have to use the store=True , keyword.
Rewrite your field definition as,
product_id = fields.Many2one('product.product', related='order_line.product_id', string='Product', store=True)
and uninstall and install the module.

How to sort by sum of field of a relation on sails.js?

I searched a lot about sorting elements by sum of votes (in another model), like I do in SQL here :
SELECT item.* FROM item
LEFT JOIN (
SELECT
vote.item,
SUM(vote.value) AS vote.rating
FROM vote
GROUP BY vote.item
) AS res ON item.id = vote.item
ORDER BY res.rating DESC
Is there a way to do it via waterline methods ?
I think you can't do the left join with simple waterline methods, but you can use the .query method to execute your raw SQL syntax.
Sails MySQL adapter makes sum('field') conflict with sort('field'). It will generate SQL query like:
SELECT SUM(table.field) AS field FROM table ORDER BY table.field;
But I want:
SELECT SUM(table.field) AS field FROM table ORDER BY field;
It same as:
SELECT SUM(table.field) AS f FROM table ORDER BY f;
My solution is using lodash.sortBy() to process results. https://lodash.com/docs/4.16.4#sortBy

subsonic join to nested selected statement

Is it possible to do a join to a nested select statement in SubSonic 2.2? I am having trouble with this because I have to do a group by...
I.E.
select conn_company.company_name, SUM(t.quote_grand_total) as 'SUM' from conn_company
inner join
(SELECT *
FROM [dbo].[QuoteDBAll]
where [dbo].[QuoteDBAll].[quote_status_id] = 1
AND [dbo].[QuoteDBAll].[quote_ca_modified_date] BETWEEN '12/1/2000' AND '12/1/2009'
) t
on conn_company.company_id = t.company_id
group by company_name
having company_name = 'JACK'
Unfortunately you're not going to be able to convert that SQL into SubSonic 2 syntax. I would suggest you create a sql view based on your query and get SubSonic to generate a model from that instead.

Resources