Sqlalchemy:为什么使用字符串主键查询对整数类型的主键有效?
Sqlalchemy: Why does querying with a string primary key work for an primary key of type integer?
我的模型被定义为主键是一个整数
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(100), nullable=False, unique=True)
email = db.Column(db.String(100), nullable=False)
password_hash = db.Column(db.String(200), nullable=False)
这两个 return 用户正确:
User.query.get("1")
User.query.get(1)
为什么我可以使用字符串来查询整数列?
来自文档:表示主键的标量、元组或字典。 -> 字符串计数
作为标量?我不确定标量在这种情况下是什么意思。
我正在使用 MySql 数据库。
这会自动生成 mysql,请参阅 manual
When an operator is used with operands of different types, type conversion occurs to make the operands compatible. Some conversions occur implicitly. For example, MySQL automatically converts strings to numbers as necessary, and vice versa.
我的模型被定义为主键是一个整数
class User(UserMixin, db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(100), nullable=False, unique=True)
email = db.Column(db.String(100), nullable=False)
password_hash = db.Column(db.String(200), nullable=False)
这两个 return 用户正确:
User.query.get("1")
User.query.get(1)
为什么我可以使用字符串来查询整数列? 来自文档:表示主键的标量、元组或字典。 -> 字符串计数 作为标量?我不确定标量在这种情况下是什么意思。
我正在使用 MySql 数据库。
这会自动生成 mysql,请参阅 manual
When an operator is used with operands of different types, type conversion occurs to make the operands compatible. Some conversions occur implicitly. For example, MySQL automatically converts strings to numbers as necessary, and vice versa.