1

In a flask application I create my schema with

db.create_all()

some of the tables should not be created, they are managed externally to the application, how can I tell SqlAlchemy to not create them ?

Max L.
  • 9,774
  • 15
  • 56
  • 86

1 Answers1

3

The command db.create_all() will only create models for those classes that you import into the script you use to run that command. For example, lets say that my models.py file has two classes:

class User(db.Model):

and

class Address(db.Model):

In the script where I run db.create_all, if my file looks like:

from models import User
db.create_all()

my app will only create the User model. Conversely, if my file looks like:

from models import User, Address
db.create_all()

both the User and Address models will be created.

Jason B
  • 7,097
  • 8
  • 38
  • 49
  • I think this is incorrect because in the module where I call create_all, no model objects are imported. I seems that sqlalchemy keeps track of model objects that are declared to maintain the list that it uses for create_all ... – Max L. Apr 14 '14 at 19:04
  • You can see places like here: http://stackoverflow.com/questions/20744277/sqlalchemy-create-all-does-not-create-tables that what I explained makes sense. Perhaps it is another error. Can you post the code you are using in that module? – Jason B Apr 14 '14 at 19:11
  • Ok, I get it, it is indeed the import that cause the model classes to be declared and I could use this to generate only part of them.. Thanks – Max L. Apr 14 '14 at 19:25
  • I want to add that you don't need to explicitly import the models in the same file. If they are being imported from another module that you have imported, it still is valid as long as it's before `db.create_all`. – BcK Dec 29 '21 at 13:23