tortoise-orm icon indicating copy to clipboard operation
tortoise-orm copied to clipboard

"Port is not an integer" when init connection

Open imaxwen opened this issue 4 years ago • 6 comments

Describe the bug When init a connection , throws tortoise.exceptions.ConfigurationError: Port is not an integer exception. I guess maybe it caused by the password charaters.

To Reproduce Steps to reproduce the behavior, preferaby a small code snippet.

from tortoise import Tortoise, run_async


async def init():
    # Here we create a SQLite DB using file "db.sqlite3"
    #  also specify the app name of "models"
    #  which contain models from "app.models"
    await Tortoise.init(
        db_url='mysql://root:1233onDB57#[email protected]:33060/cs_app',
        modules={'models': ['common.models']}
    )
    # Generate the schema
    await Tortoise.generate_schemas()


# run_async is a helper function to run simple async Tortoise scripts.
run_async(init())

The error message:

Traceback (most recent call last):
  File "/Users/maxwen/.conda/envs/csvw-spider/lib/python3.8/site-packages/tortoise/backends/base/config_generator.py", line 98, in expand_db_url
    if vmap.get("port") and url.port:
  File "/Users/maxwen/.conda/envs/csvw-spider/lib/python3.8/urllib/parse.py", line 177, in port
    raise ValueError(message) from None
ValueError: Port could not be cast to integer value as '1233onDB57'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/xxx/Projects/python/csvw-spider/webapi/test.py", line 19, in <module>
    run_async(init())
  File "/Users/xxx/.conda/envs/csvw-spider/lib/python3.8/site-packages/tortoise/__init__.py", line 707, in run_async
    loop.run_until_complete(coro)
  File "/Users/xxx/.conda/envs/csvw-spider/lib/python3.8/asyncio/base_events.py", line 616, in run_until_complete
    return future.result()
  File "/Users/xxx/Projects/python/csvw-spider/webapi/test.py", line 10, in init
    await Tortoise.init(
  File "/Users/xxx/.conda/envs/csvw-spider/lib/python3.8/site-packages/tortoise/__init__.py", line 558, in init
    config = generate_config(db_url, modules)
  File "/Users/xxx/.conda/envs/csvw-spider/lib/python3.8/site-packages/tortoise/backends/base/config_generator.py", line 125, in generate_config
    "connections": {_connection_label: expand_db_url(db_url, testing)},
  File "/Users/xxx/.conda/envs/csvw-spider/lib/python3.8/site-packages/tortoise/backends/base/config_generator.py", line 101, in expand_db_url
    raise ConfigurationError("Port is not an integer")
tortoise.exceptions.ConfigurationError: Port is not an integer

Expected behavior A clear and concise description of what you expected to happen.

Additional context Add any other context about the problem here.

imaxwen avatar Nov 28 '21 06:11 imaxwen

please use an normal passwd 1233onDB57#2020

the error raised by urllib/parse.py

panla avatar Nov 30 '21 07:11 panla

With all due respect, I think that's actually a normal password, this password works fine with sqlalchemy and pymysql. maybe we can parse this db_url in another way, as the urllib.parse doesn't work perfectly.

imaxwen avatar Dec 01 '21 16:12 imaxwen

I also have to connect with a password using special characters like ":" and cannot change that easily. So that is not possible?

belugame avatar Mar 08 '22 18:03 belugame

@belugame

such as

config = -{
    'connections': {
        'default': {
            'engine': 'tortoise.backends.mysql',
            'credentials': {
                'host': '127.0.0.1',
                'port': 3306,
                'user': 'root',
                'password': 'password',
                'database': 'demo',
                'minsize': 1,
                'maxsize': 1000,
                'charset': 'utf8mb4',
                'pool_recycle': 1800
            }
        }
    },
    'apps': {
        'models': {
            'models': [
                'aerich.models',
                'apps.models.models'
            ],
            'default_connection': 'default',
        }
    },
    'use_tz': False,
    'timezone': 'Asia/Shanghai'
}

Tortoise.init(config=config)

the origin source code tortoise/__init__.py class Tortoise .init()

panla avatar Mar 09 '22 14:03 panla

Sorry, that was my mistake. I tried to use tortoise-orm with MSSQL, so the special chars were not the problem.

belugame avatar Mar 09 '22 15:03 belugame

I faced with the same problem when try to initialize mysql db using db url. If I try with config, it works.

Achilles0509 avatar Mar 22 '22 15:03 Achilles0509