Skip to main content

Insert row

To insert data, you can call insert function like this:

from abstra.tables import insert

inserted = insert("users", {
"name": "Michael Scott",
"email": "michael.scott@dundermifflin.com"
})
inserted # {"id": 123, "name": "Michael Scott", "email": "michael.scott@dundermifflin.com"}

Which is equivalent to

INSERT INTO users (name, email)
VALUES ('Michael Scott', 'michael.scott@dundermifflin.com')
RETURNING *

Using more types

Python's date, datetime, dict, tuple, set and list objects are serialized by default:

from abstra.tables import insert
import datetime

inserted = insert("episodes", {
"name": "Goodbye, Michael",
"aired_at": datetime.date(2011, 4, 28)
"trivia": {
"imdb_rating": 9.8,
}
})
inserted # {"name": "Goodbye, Michael", "aired_at": "2011-04-28", "trivia": {"imdb_rating": 9.8}}

Using dataclass

from dataclasses import dataclass
from abstra.tables import insert

@dataclass
class User:
name: str
email: str

user = User(
name="Michael Scott",
email="michael.scott@dundermifflin.com"
)

insert("users", user)

Using PageResponse

from abstra.forms import Page
from abstra.tables import insert

user = Page()\
.read("What's your name?", key="name")\
.read_email("What's your email?", key="email")\
.run()

insert("users", user)

Using StepResponse

from abstra.forms import Page, run_steps
from abstra.tables import insert

user_page = Page()\
.read("What's your name?", key="name")\
.read_email("What's your email?", key="email")

def register(step_response):
insert("users", step_response)

registered_page = Page().display("You're registered")


run_steps([
user_page,
register,
registered_page
])

Multiple rows

The insert function also allow multiple rows at once sending a list of dictionaries.

from abstra.tables import insert

inserted = insert("users", [{
"name": "Michael Scott",
"email": "michael.scott@dundermifflin.com"
},{
"name": "Jim Halpert",
"email": "jim.halpert@dundermifflin.com"
}])
inserted
# [
# {"id": 123, "name": "Michael Scott", "email": "michael.scott@dundermifflin.com"},
# {"id": 124, "name": "Jim Halpert", "email": "jim.halpert@dundermifflin.com"}
# ]

Difference between None and not set

When using insert, it will create columns based on the keys in the dictionaries you pass as parameters. If a key is missing in any dictionary, it will use the default value for that column. If a value is None, it will be mapped as SQL NULL.

For example, if the default position in the employees table is 'Temp', this behavior will apply accordingly.

from abstra.tables import insert

inserted = insert("employees", [{
"name": "Pam Beesly",
"position": "Receptionist",
"email": "pam.beesly@dundermifflin.com"
}, {
"name": "Jan Levinson",
"position": None,
"email": "jan.levinson@dundermifflin.com"
}, {
"name": "Ryan Howard",
"email": "ryan.howard@dundermifflin.com"
}])
inserted
# [
# {"id": 126, "name": "Pam Beesly", "position": "Receptionist", "email": "pam.beesly@dundermifflin.com"},
# {"id": 127, "name": "Jan Levinson", "position": None, "email": "jan.levinson@dundermifflin.com"},
# {"id": 129, "name": "Ryan Howard", "position": "Temp", "email": "ryan.howard@dundermifflin.com"}
# ]