The base for all string and character types. In SQL, corresponds to VARCHAR. The `length` field is usually required when the `String` type is used within a CREATE TABLE statement, as VARCHAR requires a length on most databases.
| 196 | |
| 197 | |
| 198 | class String(Concatenable, TypeEngine[str]): |
| 199 | """The base for all string and character types. |
| 200 | |
| 201 | In SQL, corresponds to VARCHAR. |
| 202 | |
| 203 | The `length` field is usually required when the `String` type is |
| 204 | used within a CREATE TABLE statement, as VARCHAR requires a length |
| 205 | on most databases. |
| 206 | |
| 207 | """ |
| 208 | |
| 209 | __visit_name__ = "string" |
| 210 | |
| 211 | operator_classes = OperatorClass.STRING |
| 212 | |
| 213 | def __init__( |
| 214 | self, |
| 215 | length: Optional[int] = None, |
| 216 | collation: Optional[str] = None, |
| 217 | ): |
| 218 | """ |
| 219 | Create a string-holding type. |
| 220 | |
| 221 | :param length: optional, a length for the column for use in |
| 222 | DDL and CAST expressions. May be safely omitted if no ``CREATE |
| 223 | TABLE`` will be issued. Certain databases may require a |
| 224 | ``length`` for use in DDL, and will raise an exception when |
| 225 | the ``CREATE TABLE`` DDL is issued if a ``VARCHAR`` |
| 226 | with no length is included. Whether the value is |
| 227 | interpreted as bytes or characters is database specific. |
| 228 | |
| 229 | :param collation: Optional, a column-level collation for |
| 230 | use in DDL and CAST expressions. Renders using the |
| 231 | COLLATE keyword supported by SQLite, MySQL, and PostgreSQL. |
| 232 | E.g.: |
| 233 | |
| 234 | .. sourcecode:: pycon+sql |
| 235 | |
| 236 | >>> from sqlalchemy import cast, select, String |
| 237 | >>> print(select(cast("some string", String(collation="utf8")))) |
| 238 | {printsql}SELECT CAST(:param_1 AS VARCHAR COLLATE utf8) AS anon_1 |
| 239 | |
| 240 | .. note:: |
| 241 | |
| 242 | In most cases, the :class:`.Unicode` or :class:`.UnicodeText` |
| 243 | datatypes should be used for a :class:`_schema.Column` that expects |
| 244 | to store non-ascii data. These datatypes will ensure that the |
| 245 | correct types are used on the database. |
| 246 | |
| 247 | """ |
| 248 | |
| 249 | self.length = length |
| 250 | self.collation = collation |
| 251 | |
| 252 | def _with_collation(self, collation): |
| 253 | new_type = self.copy() |
| 254 | new_type.collation = collation |
| 255 | return new_type |
no outgoing calls