Flask-SQLAlchemy creating schema before creating tables

God Complex

I am trying to configure a MySQL schema using Flask-SQLAlchemy. I have a schema called testdb and some tables. I will list one table, User. This code, so far, creates all of the tables needed but only when testdb already exists. Is there a way to check and create testdb before I connect?

app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:password@localhost/testdb'
db = SQLAlchemy(app)

class User(db.Model):
    userid = db.Column(db.Integer, primary_key=True, autoincrement=True)
    username = db.Column(db.String(16), unique=True, nullable=False)
    password = db.Column(db.String(16), unique=False, nullable=False)
    email = db.Column(db.String(80), unique=True, nullable=False)
    createdDate = db.Column(db.DateTime, default=datetime.utcnow, nullable=False)
    lastUpdated = db.Column(db.DateTime, default=datetime.utcnow, nullable=False)

db.create_all()

Desired command:

CREATE SCHEMA IF NOT EXISTS `testdb` ;
God Complex

I solved this thanks to @hygorxaraujo See the code below:

import sqlachemy

engine = sqlalchemy.create_engine('mysql://root:password@localhost') # connect to server
engine.execute("CREATE SCHEMA IF NOT EXISTS `testdb`;") #create db
engine.execute("USE testdb;") # select new db

app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:8milerun@localhost/testdb'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False #Turn off annoying message
db = SQLAlchemy(app)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Creating tables in Flask using SQLAlchemy does not work

From Dev

SQLalchemy: multiple tables one schema, and dynamically creating tables at startup

From Dev

Postgresql Creating a Schema with tables in it

From Dev

Creating database with SQLAlchemy in Flask

From Dev

Creating a database in flask sqlalchemy

From Dev

SQLAlchemy not creating tables

From Java

Flask-Migrate not creating tables

From Java

Hibernate is not creating Tables in PostgreSQL in given Schema

From Dev

Creating a basic Table with Flask-SQLAlchemy

From Dev

Flask SQLAlchemy Association Table: error creating backref

From Dev

Creating a query with few related tables in Pyramid with SQLAlchemy

From Dev

Check MySql credentials before creating tables

From Dev

MySQL information_schema.tables is not creating new entries

From Dev

Flask SQLAlchemy db.create_all() not creating database

From Dev

Creating Flask-SQLAlchemy instance with metadata from Blueprint

From Dev

Creating a Function That Automaticaly Knows Whether to Insert or Update in Flask, SqlAlchemy, Python

From Dev

Creating relationship between models when in separate files FLASK, SQLalchemy

From Dev

Integrity Error when creating multiple records with Flask SqlAlchemy

From Dev

Sqlalchemy Foreign key relationship error while creating tables

From Dev

SQLAlchemy creating tables in all engines despite concrete binds

From Dev

Dynamically creating schema.sql before Spring context

From Dev

PL/SQL: Check if there is sufficient space in schema before creating table

From Dev

db.create_all() not creating tables in Flask-SQLAclchemy

From Dev

Async before and after for creating and dropping scala slick tables in scalatest

From Dev

Creating a ERD from schema?

From Dev

Creating a Hive schema in PySpark

From Dev

Creating a dictionary schema with Mongoose

From Dev

Dynamically creating a schema in Realm

From Dev

Creating schema with mongoose and typescript

Related Related

  1. 1

    Creating tables in Flask using SQLAlchemy does not work

  2. 2

    SQLalchemy: multiple tables one schema, and dynamically creating tables at startup

  3. 3

    Postgresql Creating a Schema with tables in it

  4. 4

    Creating database with SQLAlchemy in Flask

  5. 5

    Creating a database in flask sqlalchemy

  6. 6

    SQLAlchemy not creating tables

  7. 7

    Flask-Migrate not creating tables

  8. 8

    Hibernate is not creating Tables in PostgreSQL in given Schema

  9. 9

    Creating a basic Table with Flask-SQLAlchemy

  10. 10

    Flask SQLAlchemy Association Table: error creating backref

  11. 11

    Creating a query with few related tables in Pyramid with SQLAlchemy

  12. 12

    Check MySql credentials before creating tables

  13. 13

    MySQL information_schema.tables is not creating new entries

  14. 14

    Flask SQLAlchemy db.create_all() not creating database

  15. 15

    Creating Flask-SQLAlchemy instance with metadata from Blueprint

  16. 16

    Creating a Function That Automaticaly Knows Whether to Insert or Update in Flask, SqlAlchemy, Python

  17. 17

    Creating relationship between models when in separate files FLASK, SQLalchemy

  18. 18

    Integrity Error when creating multiple records with Flask SqlAlchemy

  19. 19

    Sqlalchemy Foreign key relationship error while creating tables

  20. 20

    SQLAlchemy creating tables in all engines despite concrete binds

  21. 21

    Dynamically creating schema.sql before Spring context

  22. 22

    PL/SQL: Check if there is sufficient space in schema before creating table

  23. 23

    db.create_all() not creating tables in Flask-SQLAclchemy

  24. 24

    Async before and after for creating and dropping scala slick tables in scalatest

  25. 25

    Creating a ERD from schema?

  26. 26

    Creating a Hive schema in PySpark

  27. 27

    Creating a dictionary schema with Mongoose

  28. 28

    Dynamically creating a schema in Realm

  29. 29

    Creating schema with mongoose and typescript

HotTag

Archive