Rust checks type constraints at compile time. A variable’s type must be declared or inferred; if inference fails or types conflict, compilation stops with a diagnostic. This prevents a class of runtime errors. section 1.2.2
Type safety is one part of program safety, distinct from memory safety and thread safety. In a changing web service, strong typing and compiler checks help make API and data-model refactoring safer. section 1.2.2 section 1.3.1
The compile-time checks extend into database work through SQLx. It checks raw SQL queries against PostgreSQL tables and columns, so a missing table, missing column, or schema mismatch can stop compilation before an API runs. section 4.2.1 section 6.3.4
Add the schema before compiling the API
When adding tutor APIs, create the tutor table in `src/iter5/dbscripts/tutor-course.sql` before compiling the application logic. The script defines columns such as `tutor_id` and `tutor_name`; SQLx checks that the SQL statements match the resulting table description. section 6.3.4
/* Drop tables if they already exist*/
drop table if exists ezy_course_c6 cascade; #1
drop table if exists ezy_tutor_c6;
/* Create tables. */
create table ezy_tutor_c6 ( #2
tutor_id serial primary key,
tutor_name varchar(200) not null,
tutor_pic_url varchar(200) not null,
tutor_profile varchar(2000) not null
);
create table ezy_course_c6 #3
(
course_id serial primary key,
tutor_id INT not null,
course_name varchar(140) not null,
course_description varchar(2000),
course_format varchar(30),
course_structure varchar(200),
course_duration varchar(30),
course_price INT,
course_language varchar(30),
course_level varchar(30),
posted_time TIMESTAMP default now(),
CONSTRAINT fk_tutor #4
FOREIGN KEY(tutor_id) #4
REFERENCES ezy_tutor_c6(tutor_id) #4
ON DELETE cascade
);
grant all privileges on table ezy_tutor_c6 to <username>; #5
grant all privileges on table ezy_course_c6 to <username>;
grant all privileges on all sequences in schema public to <username>;;
/* Load seed data for testing */ #6
insert into ezy_tutor_c6(tutor_id, tutor_name, tutor_pic_url,tutor_profile)
values(1,'Merlene','http://s3.amazon.aws.com/pic1',
'Merlene is an experienced finance professional');
insert into ezy_tutor_c6(tutor_id, tutor_name, tutor_pic_url,tutor_profile)
values(2,'Frank','http://s3.amazon.aws.com/pic2',
'Frank is an expert nuclear engineer');
insert into ezy_course_c6
(course_id,tutor_id, course_name,course_level, posted_time)
values(1, 1, 'First course', 'Beginner' , '2021-04-12 05:40:00');
insert into ezy_course_c6
(course_id, tutor_id, course_name, course_format, posted_time)
values(2, 1, 'Second course', 'ebook', '2021-04-12 05:45:00');
The SQL script shows the tutor and course table definitions that must exist for compile-time query checks to pass. section 6.3.4







