docs: add an example of a nullable ORM field (#20292)

This commit is contained in:
Juan de Bruin 2023-12-28 18:27:59 +02:00 committed by GitHub
parent eedf5876b9
commit d816f0c2fd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -4980,7 +4980,7 @@ struct Customer {
id int @[primary; sql: serial] // a field named `id` of integer type must be the first field id int @[primary; sql: serial] // a field named `id` of integer type must be the first field
name string name string
nr_orders int nr_orders int
country string country ?string
} }
db := sqlite.connect('customers.db')! db := sqlite.connect('customers.db')!
@ -4989,8 +4989,8 @@ db := sqlite.connect('customers.db')!
// CREATE TABLE IF NOT EXISTS `Customer` ( // CREATE TABLE IF NOT EXISTS `Customer` (
// `id` INTEGER PRIMARY KEY, // `id` INTEGER PRIMARY KEY,
// `name` TEXT NOT NULL, // `name` TEXT NOT NULL,
// `nr_orders` INTEGER, // `nr_orders` INTEGER NOT NULL,
// `country` TEXT NOT NULL // `country` TEXT
// ) // )
sql db { sql db {
create table Customer create table Customer
@ -5015,6 +5015,15 @@ sql db {
insert us_customer into Customer insert us_customer into Customer
}! }!
none_country_customer := Customer{
name: 'Dennis'
country: none
nr_orders: 2
}
sql db {
insert none_country_customer into Customer
}!
// update a customer: // update a customer:
sql db { sql db {
update Customer set nr_orders = nr_orders + 1 where name == 'Bob' update Customer set nr_orders = nr_orders + 1 where name == 'Bob'
@ -5030,9 +5039,17 @@ println('number of all customers: ${nr_customers}')
uk_customers := sql db { uk_customers := sql db {
select from Customer where country == 'uk' && nr_orders > 0 select from Customer where country == 'uk' && nr_orders > 0
}! }!
println('We found a total of ${uk_customers.len} customers, that match the query.') println('We found a total of ${uk_customers.len} customers matching the query.')
for customer in uk_customers { for c in uk_customers {
println('customer: ${customer.id}, ${customer.name}, ${customer.country}, ${customer.nr_orders}') println('customer: ${c.id}, ${c.name}, ${c.country}, ${c.nr_orders}')
}
none_country_customers := sql db {
select from Customer where country is none
}!
println('We found a total of ${none_country_customers.len} customers, with no country set.')
for c in none_country_customers {
println('customer: ${c.id}, ${c.name}, ${c.country}, ${c.nr_orders}')
} }
// delete a customer // delete a customer