Comparing lists in DBT jinja macro inside for loop - jinja2

I am storing output of each column in a list for the below query
select table,
columns,
flag
from {{ model }}
for each table I need to compare the flag. how do i get flag in list for the corresponding table
{% for n in table %}
{%- if flag[n] == 'False' %}
I tried as above its not working its returning null value

It's important to understand that jinja is a templating language. In dbt, we use jinja to automate the creation of sql that then gets executed in our data warehouse. Accordingly, data from your data warehouse does not (typically) flow through your jinja code.
flag is a reference to a column in your database, so to evaluate the contents of flag, you need to write sql to do that job.
The only exception is if you are using run_query or a similar macro to pull the data from your database into your jinja context at model-compile time. But I don't see any reference to that in your code (and frankly that's a complicating detail that's only relevant if you are trying to write SQL dynamically). If you are just trying to compare the value from a record in your database as part of your model, just do it in sql.

Related

ProgrammingError at /user_booking not all arguments converted during bytes formatting

The idea is to display user's booking information like booking_name, package etc. I am fetching the data from database in my views.py like this.
def user_booking(request):
id = request.session['u_id']
booking_data = Booking.objects.raw('select * from booking WHERE id = %s',id)
return render(request,'user_booking.html',{'view_data':booking_data})
Now in user_booking.html is displaying data. I have used a proper template to display data in tabular format. Problem is when some data is returned from data base it works perfect. When there is a query for which no data can be found the page breaks and shows
"ProgrammingError at /user_booking not all arguments converted during bytes formatting"
Instead of all the formatting I resorted to basic code which looks like this
<html>
{% for b in view_data %}
{{b.b_name}}
{% endfor %}
</html>
It gives me the name "ajay" as output and does not break the page, also note that record for ajay's ID is present in my database. Now if ID is changed and then I refresh the lage again it gives me error.
What I am trying to do is if there is any data coming from the database then display it otherwise just display "no data here" msg.
How do I achieve it?

dbt Snapshot using Variable as source & DAG lineage

I've got 3 main questions;
I've created a Snapshot, but instead of referencing a Source using the Source() function I have used variables that can be passed from the command line.
(My plan is to get Azure Data Factory to run a dbt Snapshot on the end of importing a source).
At the moment I'm testing in Visual Studio Code running from Powershell using the command
dbt run --vars '{"SourceSch": "SourceExampleSchema", "SourceTble": "SrcTbl"}'
In the snapshot I've got the following;
{% snapshot SrcTbl %}
At the moment I can't get "SrcTbl" to populate from the variable so I've had to hard code it.
I've tried the following results in an object called "var".
{% snapshot var("SourceTble") %}
Can anyone tell me how to reference the variable in the snapshot name?
When the docs are served only the snapshot table is a node, the source isn't listed.
Is this the default behaviour or is it because I'm not calling the Source() function?
How would I get the DAG to pick up the source going into the Snapshot?
Then I'm looking to expand this through Jinja so that it creates a snapshot for each "SourceTble".
3. How would I expand it if I had the following structure;
Database
-> Schema 1
-> Table 1
-> Schema 2
-> Table 2
-> Table 3
I think it would be something like;
dbt run --vars '{"SourceSch": ["Schema1", "SourceTble": "Table1", "Schema2", "SourceTble": ["Table2", "Table3"]]}'
But I'm confused on how to use Jinja to create a .sql file for each table using the one as a base.
Any help is appreciated.
Thanks,
Dan

How do I return a value from database to template?

I have a database that uses formset to help store data. How do I get the ‘value’ of the numbers of data in the template? For example, I got a queryset of {[Item 1],[ Item 2]}. How do I get the value of 2 in the template to tell me there's 2 items? I want to use this value to control the amount of stuff I can clone with a click of the button. I'm using Django for the web
You just ask |length of the queryset, like:
{{ queryset|length }}
will be returning the number of element

How can I access ALL model attributes from two tables joined together using django ORM

I want to access all values from two related tables that I joined together based on one of the tables attributes:
obj = A.objects.filter(B__sample='sample_name').select_related()
but when i do:
{% for o in obj %}
{{o.sample}}
{{o.results}}
{{o.qc}}
{% endfor %}
only o.results and o.qc (from table A) are returned, o.sample is not returned (from table B)
how do i access all of the values from table A and B from my queryset object?
You've misunderstood how models work in Django.
sample is an attribute from model B, and will always be that. Django won't ever add it as a direct attribute to model A; that would be confusing.
You still access it via an instance of model B; the magic that Django gives you is that o.B (or whatever your ForeignKey is called) will access that model B instance, so you can do o.B.sample. Since you've used select_related, that won't incur another database hit.
obj = A.objects.filter(B__sample='sample_name').select_related('b')
try this , with small foreign model name

SSIS 2012 Full Result Set to set variables

I'm trying to create an SSIS package that reads a mapping table that contains foreign key information and tables they point to and store the full result set to be used to populate 7 columns representing columns in the result set that is then used to update an xxxSID column on 6 servers.
I'm stuck! Please help.
I've created the SQL Task with query to build the result set and mapped to object variable SidMap and the task runs successfully however, I don't know where to go from there. Some blogs say create a ForEachLoop Container and map the object variable to the collection which I've done. I've also created string variables representing the 7 columns but don't know how to populate them.
The blogs I've read so far suggest this can only be done from a Script task. Is that true? If so how is it done?
Another user posted a question that sounded like he may be doing the same or very similar thing using a SQL Task but I didn't see how he was populating the column object variables and then converting data into string variables.
SSIS Result set, Foreachloop and Variable
Currently I'm updating tables manually using a cursor. If anyone cares to see the code I can post it but didn't think it relevant to the question other than providing a clear picture of what I'm doing.
I would create a For Each Loop Container using the Foreach ADO Enumerator, and map the object variable to the collection. I would map the 7 string variables on the Variable Mappings page.
This process is documented in detail here:
http://technet.microsoft.com/en-us/library/cc879316.aspx
A common "gotcha" is mismatched datatypes between the result set and the Variables. To avoid this I always wrap CAST ( ... AS NVARCHAR ( 4000 ) ) or similar around the columns in the dataflow that produces the dataset, and all my receiving Variables are String datatype.