Flask开发中SQLAlchemy连接MSSQL的引擎与db变量映射问题求助
Hey there! Let's break down your questions and fix this step by step.
First: No, you can't replace db = SQLAlchemy(app) with db = engine.connect()
The SQLAlchemy(app) instance is the core of Flask-SQLAlchemy's ORM toolkit—it wraps SQLAlchemy's core functionality, handles session management, and lets you define models with db.Model. On the other hand, engine.connect() just returns a raw database connection object, which doesn't have any of the ORM features you need for your User and Post models. So that approach won't work for your Flask app.
Fix 1: Use the correct URL format for SQLALCHEMY_DATABASE_URI
Your current URI uses the raw ODBC string format, but Flask-SQLAlchemy expects a URL-style connection string (compatible with SQLAlchemy's core). Your manual create_engine call uses the right format, but you need to adjust your app config to match it. Also, note that if your password contains special characters like @, you need to URL-encode them (replace @ with %40).
Here's the corrected config line for your case:
app.config['SQLALCHEMY_DATABASE_URI'] = 'mssql+pyodbc://sa:abc%4099@localhost/Test?driver=SQL+Server+Native+Client+17.0'
(I encoded the @ in your password abc@99 to %40—this is critical because @ in the URL separates the username/password from the host.)
With this config, Flask-SQLAlchemy will automatically create the correct engine for you—no need to manually call create_engine unless you have specific customizations.
Fix 2: If you must use a manually created engine (optional)
If you need to customize the engine (like setting echo=True as you did), you can initialize db first, then replace its engine with your custom one:
from sqlalchemy import create_engine # Your custom engine engine = create_engine('mssql+pyodbc://sa:abc%4099@localhost/Test?driver=SQL+Server+Native+Client+17.0', echo=True) # Initialize Flask-SQLAlchemy db = SQLAlchemy(app) # Replace the default engine with your custom one db.engine = engine
That said, for most cases, just setting the correct SQLALCHEMY_DATABASE_URI is enough—you can enable echo logging via Flask-SQLAlchemy's config if needed, but your manual engine approach works too.
Fix 3: Correct typos in your model definitions
I noticed a small typo in your models: __tablename_ (with a single trailing underscore) should be __tablename__ (double trailing underscore). This tells SQLAlchemy the exact table name to map to—without the correct syntax, SQLAlchemy will generate a default table name instead.
Here's the corrected User model example:
class User(db.Model): __tablename__ = 'User' # Fixed trailing underscores id = db.Column(db.Integer, primary_key=True) # ... rest of your fields
Do the same for your Post model.
Full Corrected Code
Here's your code with all the fixes applied:
from datetime import datetime from flask import Flask, render_template, url_for, flash, redirect from flask_sqlalchemy import SQLAlchemy from forms import RegistrationForm, LoginForm app = Flask(__name__) # Corrected database URI with URL-encoded password app.config['SQLALCHEMY_DATABASE_URI'] = 'mssql+pyodbc://sa:abc%4099@localhost/Test?driver=SQL+Server+Native+Client+17.0' app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False # Optional: If you want echo logging, you can set it here or use a custom engine # app.config['SQLALCHEMY_ECHO'] = True db = SQLAlchemy(app) class User(db.Model): __tablename__ = 'User' # Fixed typo id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(20), unique=True, nullable=False) email = db.Column(db.String(120), unique=True, nullable=False) image_file = db.Column(db.String(20), nullable=False, default='default.jpg') password = db.Column(db.String(60), nullable=False) posts = db.relationship('Post', backref='author', lazy=True) def __repr__(self): return f"User('{self.username}', '{self.email}', '{self.image_file}')" class Post(db.Model): __tablename__ = 'Post' # Fixed typo id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), nullable=False) date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) content = db.Column(db.Text, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False) def __repr__(self): return f"Post('{self.title}', '{self.date_posted}')" posts = [ { 'author': 'Corey Schafer', 'title': 'Blog Post 1', 'content': 'First post content', 'date_posted': 'April 20, 2018' }, { 'author': 'Jane Doe', 'title': 'Blog Post 2', 'content': 'Second post content', 'date_posted': 'April 21, 2018' } ] @app.route("/") @app.route("/home") def home(): return render_template('home.html', posts=posts) @app.route("/about") def about(): return render_template('about.html', title='About') @app.route("/register", methods=['GET', 'POST']) def register(): form = RegistrationForm() if form.validate_on_submit(): flash(f'Account created for {form.username.data}!', 'success') return redirect(url_for('home')) return render_template('register.html', title='Register', form=form) @app.route("/login", methods=['GET', 'POST']) def login(): form = LoginForm() if form.validate_on_submit(): if form.email.data == 'admin@blog.com' and form.password.data == 'password': flash('You have been logged in!', 'success') return redirect(url_for('home')) else: flash('Login Unsuccessful. Please check username and password', 'danger') return render_template('login.html', title='Login', form=form) if __name__ == '__main__': app.run(debug=True)
Final Notes
- Always use URL-style connection strings for Flask-SQLAlchemy to avoid parsing issues.
- The
SQLAlchemy(app)instance is essential for using ORM models—don't replace it with a raw connection. - Fix typos like
__tablename_to ensure your models map correctly to database tables.
内容的提问来源于stack exchange,提问作者Learning_Learning

