0Pricing
Flask Academy · Lekcja

Tworzenie schematu za pomocą create_all

Generuj tabele na podstawie definicji modeli.

Tworzenie schematu za pomocą create_all to bezpłatna lekcja Flask Academy na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Flask Academy, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Flask Academy zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

From Models to Tables

Your models describe tables, but the database file is still empty. You need one step to turn declarations into real tables. 🏗️

Meet create_all

db.create_all() scans every model it knows and issues the CREATE TABLE statements for you. One call builds the whole schema.

db.create_all()

It Needs App Context

create_all must run inside an application context so it can find your config and the right database connection.

with app.app_context():
    db.create_all()

Import Models First

create_all only builds tables for models that are imported. If a model file is never loaded, its table silently goes missing.

Run It From a Shell

A clean way to set up the database is the flask shell, where the app context is already active and ready.

flask shell

It Is Safe to Re-run

create_all skips tables that already exist, so calling it twice will not crash. It simply creates whatever is missing.

It Will Not Alter Tables

Here is the catch: create_all never changes an existing table. Add a new column and the old table stays exactly as it was.

Wipe Everything with drop_all

Need a fresh start in development? db.drop_all() deletes every table and its data, so handle it with real care.

db.drop_all()

Reset Pattern in Dev

A common dev reset is drop then create. It rebuilds clean tables, but it also erases all data, so never do this in production.

db.drop_all()
db.create_all()

Verify the File Appeared

With SQLite, a database file like app.db shows up after create_all. Seeing it confirms your tables were actually built.

Next Step: Migrations

Because create_all cannot evolve a schema, real projects switch to migrations as soon as the model starts changing over time.

Quick Check

You added a new column to an existing model. What does create_all do?

Recap

You built tables with db.create_all() inside an app context, learned it never alters existing tables, and met drop_all. 🎉

Często zadawane pytania

Czy lekcja „Tworzenie schematu za pomocą create_all” jest bezpłatna?

Tak — pełny tekst „Tworzenie schematu za pomocą create_all” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Flask Academy, przejdź na CoddyKit PRO. Kurs Flask Academy zawiera 4 lekcji w sumie.

Co nauczysz się w „Tworzenie schematu za pomocą create_all”?

Generuj tabele na podstawie definicji modeli. Ćwiczysz Flask Academy z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Flask Academy?

Nie wymagamy żadnego doświadczenia. Flask Academy w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.

Ile czasu zajmuje lekcja „Tworzenie schematu za pomocą create_all”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Flask Academy?

Tak. Każda lekcja Flask Academy zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Instalowanie i konfigurowanie rozszerzenia
  2. Definiowanie pierwszej klasy modelu
  3. Kolumny, typy i ograniczenia
  4. Tworzenie schematu za pomocą create_all
← Powrót do Flask Academy