InvalidRequestError: VARCHAR requires a length on dialect mysql

20,827

Add the length to your String column:

src_address = Column(String(16), index=True)
Share:
20,827
liv2hak
Author by

liv2hak

Updated on May 10, 2020

Comments

  • liv2hak
    liv2hak about 4 years

    I am trying to create a remote database using mysql on an Ubuntu machine running 12.04.

    It has a root user with remote login enabled and no password.I have started the server.

    output of

    sudo netstat -tap | grep mysql
    

    shows

    tcp        0      0 localhost:mysql         *:*                     LISTEN      13246/mysqld
    

    I have created a database called nwtopology using (as mentioned root doesn't have a password yet.)

     create database nwtopology
     grant all privileges on *.* to [email protected]
     FLUSH PRIVILEGES;
    

    From the client machine that also runs Ubuntu 12.04 I use a python script to connect to the remote mysql database using sqlalchemy.

    from pox.core import core
    import pox.openflow.libopenflow_01 as of
    import re
    import datetime
    import time
    from sqlalchemy import create_engine, ForeignKey
    from sqlalchemy import Column, Date, Integer, String
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy.orm import relationship, backref
    from sqlalchemy import create_engine
    from sqlalchemy.orm import sessionmaker
    from sqlalchemy.sql.expression import exists
    
    log = core.getLogger()
    engine = create_engine('mysql://[email protected]/nwtopology', echo=False)
    Base = declarative_base()
    Session = sessionmaker(bind=engine)
    session = Session()
    
    class SourcetoPort(Base):
        """"""
        __tablename__ = 'source_to_port'
        id = Column(Integer, primary_key=True)
        port_no        = Column(Integer)
        src_address    = Column(String,index=True)
    
        #-----------------------------------------
        def __init__(self, src_address,port_no):
            """"""
            self.src_address = src_address
            self.port_no     = port_no
    
    
    #create tables
    Base.metadata.create_all(engine)
    

    The last line

    Base.metadata.create_all(engine)
    

    returns the error

     File "/usr/lib/python2.7/dist-packages/sqlalchemy/sql/compiler.py", line 1699, in visit_string
        return self.visit_VARCHAR(type_)
      File "/usr/lib/python2.7/dist-packages/sqlalchemy/dialects/mysql/base.py", line 1654, in visit_VARCHAR
        self.dialect.name)
    InvalidRequestError: VARCHAR requires a length on dialect mysql
    

    What does this mean? how can I set VARCHAR length on mysql? I am very new to sqlalchemy and mysql.

  • Leo
    Leo over 5 years
    Thank you! For some reason I didn't need to specify the length in my local environment, but it seems Google Cloud SQL required it.