Modern SQL: Lateral

Lateral is the “for each” loop of SQL

.. Lateral is great for Top-N subqueries

.. In PostgreSQL “With” view are more like materialized views

With deleted_rows AS (

Delete from source_tbl

Returning *

)

Insert into destination_tbl

Select * from deleted_rows;

Trying JSON in Django and PostgreSQL (and compare with MongoDB)

class Product(models.Model):
    name = models.CharField(max_length=100)
    category = models.ForeignKey(Category)
    price = models.IntegerField()
    attributes = JSONField()

    def __str__(self):
        return self.name

 

Product.objects.create(name='Bamboo tshirt', category=tshirt, price=120, attributes={
    'colors': ['white', 'yellow'],
    'sizes': ['M', 'L', 'XL'],
    'model': 'poet',
    'material': 'bamboo',
})