From 450477cf75e5cb4163fde87fed74789a2267f4bb Mon Sep 17 00:00:00 2001 From: VOICE1 Date: Mon, 16 Aug 2021 19:07:28 -0700 Subject: [PATCH 1/2] Updated common-parameters.md Added notation about creating column name aliaes --- docs/fields/common-parameters.md | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/docs/fields/common-parameters.md b/docs/fields/common-parameters.md index 79dde76..b07f00c 100644 --- a/docs/fields/common-parameters.md +++ b/docs/fields/common-parameters.md @@ -101,7 +101,23 @@ Sample usage: !!!info `server_default` is passed straight to sqlalchemy table definition so you can read more in [server default][server default] sqlalchemy documentation - + +## name + +`name`: `Any` = `None` -> Defaults to None + +Allows you to specify a column name alias to be used. Useful for existing database structures that use a reserved keyword. + +Take for example the snippet below. `from`, being a reserved word in python, will prevent you from creating a model with that column name. Changing the model name + +to `from_` and adding the parameter `name='from'` will cause ormar to use `from` for the database column name. + +``` + ... + from_: str = ormar.String(max_length=15, name='from') + ... +``` + ## index `index`: `bool` = `False` -> by default False, @@ -147,4 +163,4 @@ Used in pydantic only. [relations]: ../relations/index.md [queries]: ../queries/index.md [pydantic]: https://pydantic-docs.helpmanual.io/usage/types/#constrained-types -[server default]: https://docs.sqlalchemy.org/en/13/core/defaults.html#server-invoked-ddl-explicit-default-expressions \ No newline at end of file +[server default]: https://docs.sqlalchemy.org/en/13/core/defaults.html#server-invoked-ddl-explicit-default-expressions From 7d0826392ee868f8fe1f10bd1540aee104b0a59f Mon Sep 17 00:00:00 2001 From: collerek Date: Tue, 17 Aug 2021 11:40:31 +0200 Subject: [PATCH 2/2] Update common-parameters.md add also fk sample --- docs/fields/common-parameters.md | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/docs/fields/common-parameters.md b/docs/fields/common-parameters.md index b07f00c..d5a4c0d 100644 --- a/docs/fields/common-parameters.md +++ b/docs/fields/common-parameters.md @@ -104,18 +104,28 @@ Sample usage: ## name -`name`: `Any` = `None` -> Defaults to None +`name`: `str` = `None` -> defaults to None -Allows you to specify a column name alias to be used. Useful for existing database structures that use a reserved keyword. +Allows you to specify a column name alias to be used. -Take for example the snippet below. `from`, being a reserved word in python, will prevent you from creating a model with that column name. Changing the model name +Useful for existing database structures that use a reserved keyword, or if you would like to use database name that is different from `ormar` field name. -to `from_` and adding the parameter `name='from'` will cause ormar to use `from` for the database column name. +Take for example the snippet below. -``` - ... +`from`, being a reserved word in python, will prevent you from creating a model with that column name. + +Changing the model name to `from_` and adding the parameter `name='from'` will cause ormar to use `from` for the database column name. + +```python + #... rest of Model cut for brevity from_: str = ormar.String(max_length=15, name='from') - ... +``` + +Similarly, you can change the foreign key column names in database, while keeping the desired relation name in ormar: + +```python + # ... rest of Model cut for brevity +album: Optional[Album] = ormar.ForeignKey(Album, name="album_id") ``` ## index