Default value ignored in non-nullable column - sqlite

I'm trying to create a new boolean, non-nullable column (with default=True) in a table with the following SQL alchemy script:
from sqlalchemy import MetaData, Table, Boolean, Column
def upgrade(migrate_engine):
meta = MetaData(bind=migrate_engine)
message_table = Table('message', meta, autoload=True)
col = Column('include_signature', Boolean(), default=True, nullable=False)
col.create(message_table)
This results in a rather puzzling error:
File "src/adhocracy/migration/versions/061_optional_massmessage_signature.py", line 7, in upgrade
col.create(message_table)
File "eggs/sqlalchemy_migrate-0.7.2-py2.6.egg/migrate/changeset/schema.py", line 528, in create
engine._run_visitor(visitorcallable, self, connection, **kwargs)
File "eggs/SQLAlchemy-0.7.10-py2.6-linux-x86_64.egg/sqlalchemy/engine/base.py", line 2302, in _run_visitor
conn._run_visitor(visitorcallable, element, **kwargs)
File "eggs/SQLAlchemy-0.7.10-py2.6-linux-x86_64.egg/sqlalchemy/engine/base.py", line 1972, in _run_visitor
**kwargs).traverse_single(element)
File "eggs/sqlalchemy_migrate-0.7.2-py2.6.egg/migrate/changeset/ansisql.py", line 53, in traverse_single
ret = super(AlterTableVisitor, self).traverse_single(elem)
File "eggs/SQLAlchemy-0.7.10-py2.6-linux-x86_64.egg/sqlalchemy/sql/visitors.py", line 106, in traverse_single
return meth(obj, **kw)
File "eggs/sqlalchemy_migrate-0.7.2-py2.6.egg/migrate/changeset/databases/sqlite.py", line 75, in visit_column
super(SQLiteColumnGenerator,self).visit_column(column)
File "eggs/sqlalchemy_migrate-0.7.2-py2.6.egg/migrate/changeset/ansisql.py", line 101, in visit_column
self.execute()
File "eggs/sqlalchemy_migrate-0.7.2-py2.6.egg/migrate/changeset/ansisql.py", line 42, in execute
return self.connection.execute(self.buffer.getvalue())
File "eggs/SQLAlchemy-0.7.10-py2.6-linux-x86_64.egg/sqlalchemy/engine/base.py", line 1449, in execute
params)
File "eggs/SQLAlchemy-0.7.10-py2.6-linux-x86_64.egg/sqlalchemy/engine/base.py", line 1628, in _execute_text
statement, parameters
File "eggs/SQLAlchemy-0.7.10-py2.6-linux-x86_64.egg/sqlalchemy/engine/base.py", line 1698, in _execute_context
context)
File "eggs/SQLAlchemy-0.7.10-py2.6-linux-x86_64.egg/sqlalchemy/engine/base.py", line 1691, in _execute_context
context)
File "eggs/SQLAlchemy-0.7.10-py2.6-linux-x86_64.egg/sqlalchemy/engine/default.py", line 331, in do_execute
cursor.execute(statement, parameters)
sqlalchemy.exc.OperationalError: (OperationalError) Cannot add a NOT NULL column
with default value NULL '\nALTER TABLE message ADD include_signature BOOLEAN NOT NULL' ()
As far as I understand, sqlalchemy is missing a default value, but I did include default=True! What is wrong here?

Use server_default="true", like that:
col = Column('include_signature', Boolean(), default=True,
server_default="true", nullable=False)
Where default is used for the class instance and server_default is used for the database column definition.

Related

sqllite argumentError absolute path

I am trying to create a sqlite database using windows and I have an error "ArgumentError: Invalid SQLite URL: sqlite://search.db" What is the absolute path syntax for creating an SQL lite db.
'sqlite://C:/Users..../search.db'
>>> from flaskapp import db
C:\Users\champ\Python_proj\main_env\lib\site-packages\flask_sqlalchemy\__init__.py:833: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning.
warnings.warn(FSADeprecationWarning(
>>> db.create_all()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\champ\Python_proj\main_env\lib\site-packages\flask_sqlalchemy\__init__.py", line 1039, in create_all
self._execute_for_all_tables(app, bind, 'create_all')
File "C:\Users\champ\Python_proj\main_env\lib\site-packages\flask_sqlalchemy\__init__.py", line 1031, in _execute_for_all_tables
op(bind=self.get_engine(app, bind), **extra)
File "C:\Users\champ\Python_proj\main_env\lib\site-packages\flask_sqlalchemy\__init__.py", line 962, in get_engine
return connector.get_engine()
File "C:\Users\champ\Python_proj\main_env\lib\site-packages\flask_sqlalchemy\__init__.py", line 556, in get_engine
self._engine = rv = self._sa.create_engine(sa_url, options)
File "C:\Users\champ\Python_proj\main_env\lib\site-packages\flask_sqlalchemy\__init__.py", line 972, in create_engine
return sqlalchemy.create_engine(sa_url, **engine_opts)
File "C:\Users\champ\Python_proj\main_env\lib\site-packages\sqlalchemy\engine\__init__.py", line 488, in create_engine
return strategy.create(*args, **kwargs)
File "C:\Users\champ\Python_proj\main_env\lib\site-packages\sqlalchemy\engine\strategies.py", line 98, in create
(cargs, cparams) = dialect.create_connect_args(u)
File "C:\Users\champ\Python_proj\main_env\lib\site-packages\sqlalchemy\dialects\sqlite\pysqlite.py", line 466, in create_connect_args
raise exc.ArgumentError(
sqlalchemy.exc.ArgumentError: Invalid SQLite URL: sqlite://search.db
Valid SQLite URL forms are:
sqlite:///:memory: (or, sqlite://)
sqlite:///relative/path/to/file.db
sqlite:////absolute/path/to/file.db

gremlin python add vertex KeyError

I'm using gremlinpython. Inserting a vertex with a property value of greater than 32-bits results in a KeyError.
g.addV('test').property('size', 2147483648).iterate()
File "/home/ec2-user/src/common/test.py", line 74, in insert_vertices
self.g.addV('test').property('size', 2147483648).iterate()
File "/home/ec2-user/venv/lib64/python3.6/dist-packages/gremlin_python/process/traversal.py", line 65, in iterate
try: self.nextTraverser()
File "/home/ec2-user/venv/lib64/python3.6/dist-packages/gremlin_python/process/traversal.py", line 70, in nextTraverser
self.traversal_strategies.apply_strategies(self)
File "/home/ec2-user/venv/lib64/python3.6/dist-packages/gremlin_python/process/traversal.py", line 506, in apply_strategies
traversal_strategy.apply(traversal)
File "/home/ec2-user/venv/lib64/python3.6/dist-packages/gremlin_python/driver/remote_connection.py", line 148, in apply
remote_traversal = self.remote_connection.submit(traversal.bytecode)
File "/home/ec2-user/venv/lib64/python3.6/dist-packages/gremlin_python/driver/driver_remote_connection.py", line 54, in submit
results = result_set.all().result()
File "/usr/lib64/python3.6/concurrent/futures/_base.py", line 432, in result
return self.__get_result()
File "/usr/lib64/python3.6/concurrent/futures/_base.py", line 384, in __get_result
raise self._exception
File "/home/ec2-user/venv/lib64/python3.6/dist-packages/gremlin_python/driver/resultset.py", line 90, in cb
f.result()
File "/usr/lib64/python3.6/concurrent/futures/_base.py", line 425, in result
return self.__get_result()
File "/usr/lib64/python3.6/concurrent/futures/_base.py", line 384, in __get_result
raise self._exception
File "/usr/lib64/python3.6/concurrent/futures/thread.py", line 56, in run
result = self.fn(*self.args, **self.kwargs)
File "/home/ec2-user/venv/lib64/python3.6/dist-packages/gremlin_python/driver/connection.py", line 80, in _receive
status_code = self._protocol.data_received(data, self._results)
File "/home/ec2-user/venv/lib64/python3.6/dist-packages/gremlin_python/driver/protocol.py", line 83, in data_received
result_set = results_dict[request_id]
KeyError: None
A value of less than 32 bits works fine
g.addV('test').property('size', 2147483647).iterate()
Casting it to float also works fine
g.addV('test').property('size', float(2147483648)).iterate()
Same behavior with a locally running gremlin server and a remote Neptune DB. It works fine from the gremlin console. So I don't think this is a server issue.
Python version - 3.6 and 3.7
gremlinpython version - 3.4.1
You explicitly need to define that number as a long() like:
from gremlin_python.statics import *
g.addV('test').property('size', long(2147483648)).iterate()

__pillar___ empty in custom module

I write a simple custom module, and would like to access the pillar data.
I test it like this (file salt/_modules/foo.py):
def bar():
assert 0, __pillar__
sls file salt/tmp/test_pillar_in_custom_module.sls:
{{ salt['foo.bar']() }}
And I execute it like this:
salt-ssh --log-leve=all foohost state.sls tmp.test_pillar_in_custom_module pillar='{systems: [foosys]}' 2> tmp/o
Relevant part of tmp/o:
Traceback (most recent call last):
File "/var/tmp/.root_dcdf8c_salt/salt-call", line 27, in <module>
salt_call()
File "/var/tmp/.root_dcdf8c_salt/pyall/salt/scripts.py", line 431, in salt_call
client.run()
File "/var/tmp/.root_dcdf8c_salt/pyall/salt/cli/call.py", line 57, in run
caller.run()
File "/var/tmp/.root_dcdf8c_salt/pyall/salt/cli/caller.py", line 138, in run
ret = self.call()
File "/var/tmp/.root_dcdf8c_salt/pyall/salt/cli/caller.py", line 237, in call
ret['return'] = self.minion.executors[fname](self.opts, data, func, args, kwargs)
File "/var/tmp/.root_dcdf8c_salt/pyall/salt/executors/direct_call.py", line 12, in execute
return func(*args, **kwargs)
File "/var/tmp/.root_dcdf8c_salt/running_data/var/cache/salt/minion/extmods/modules/foo.py", line 2, in bar
assert 0, __pillar__
AssertionError: {}
But the dict __pillar__ is empty.
I read the docs: https://docs.saltstack.com/en/latest/topics/development/modules/developing.html#pillar
The __pillar__ dictionary contains the pillar for the respective
minion.
While __pillar__ is defined for every module, it's only filled in for
some.
In my custom module it is empty.
Is there a way to access the pillar in a custom module?

"Adapter does not support geometry" exception when declaring geometry field

In my application, an "Adapter does not support geometry" exception is being thrown when attempting to create a field of type, "geometry()". For my test application, I'm using an sqlite DB (production will use postgres):
db = DAL('sqlite://storage.sqlite', pool_size = 1, fake_migrate_all= False)
The DB table in question is declared within a class, inside of a module, and contains a several fields, some of which contain location data:
from gluon.dal import Field, geoPoint, geoLine, geoPolygon
class Info(Base_Model):
def __init__(...):
try:
db.define_table('t_info',
...
Field('f_geolocation', type='geometry()',
label = current.T('Geolocation')),
Field('f_city', type='string',
label = current.T('City')),
...
except Exception as e:
...
Edit:
As per Anthony's suggestion, I've modified the DAL constructor call to the following:
db = DAL('spatialite://storage.sqlite', pool_size = 1)
It produces the following error message:
Traceback (most recent call last):
File "C:\...\web2py\gluon\restricted.py", line 227, in restricted
exec ccode in environment
File "C:/My_Stuff/Programs/web2py/applications/Proj/models/db.py", line 38, in <module>
db = DAL('spatialite://storage.sqlite', pool_size = 1)
File "C:\...\web2py\gluon\packages\dal\pydal\base.py", line 171, in __call__
obj = super(MetaDAL, cls).__call__(*args, **kwargs)
File "C:\...\web2py\gluon\packages\dal\pydal\base.py", line 457, in __init__
raise RuntimeError("Failure to connect, tried %d times:\n%s" % (attempts, tb))
RuntimeError: Failure to connect, tried 5 times:
Traceback (most recent call last):
File "C:\...\web2py\gluon\packages\dal\pydal\base.py", line 435, in __init__
self._adapter = ADAPTERS[self._dbname](**kwargs)
File "C:\...\web2py\gluon\packages\dal\pydal\adapters\base.py", line 53, in __call__
obj = super(AdapterMeta, cls).__call__(*args, **kwargs)
File "C:\...\web2py\gluon\packages\dal\pydal\adapters\sqlite.py", line 169, in __init__
if do_connect: self.reconnect()
File "C:\...\web2py\gluon\packages\dal\pydal\connection.py", line 129, in reconnect
self.after_connection_hook()
File "C:\...\web2py\gluon\packages\dal\pydal\connection.py", line 81, in after_connection_hook
self.after_connection()
File "C:\...\web2py\gluon\packages\dal\pydal\adapters\sqlite.py", line 177, in after_connection
self.execute(r'SELECT load_extension("%s");' % libspatialite)
File "C:\...\web2py\gluon\packages\dal\pydal\adapters\base.py", line 1326, in execute
return self.log_execute(*a, **b)
File "C:\...\web2py\gluon\packages\dal\pydal\adapters\base.py", line 1320, in log_execute
ret = self.cursor.execute(command, *a[1:], **b)
OperationalError: The specified module could not be found.
If you want to use geometry fields with SQLite, you must use the spatialite adapter, which makes use of the SpatialLite extension for SQLite:
db = DAL('spatialite://storage.sqlite', pool_size = 1)
Note, you must have spatialite installed for this to work.

sqlalchemy Sqlite not loading table rows

I'm using sqlite with sqlalchemy:
when I execute this command:
engine.execute("select * from teams");
<sqlalchemy.engine.base.ResultProxy object at 0x245b190>
Thus, it works as it returns a reference
I've a .py file which contain the class Team:
class Team(Base):
__tablename__ = 'teams'
id = Column(Integer,autoincrement=False, primary_key=True)
name = Column(String(255))
flag = Column(String(255))
url = Column(String(255))
I import Team from classes import
But when I try to use the ORM, i get the following error:
for instance in session.query(Team):
... print instance.id
...
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/site-packages/SQLAlchemy-0.7.7-py2.7.egg/sqlalchemy/orm/query.py", line 2224, in __iter__
return self._execute_and_instances(context)
File "/usr/lib/python2.7/site-packages/SQLAlchemy-0.7.7-py2.7.egg/sqlalchemy/orm/query.py", line 2239, in _execute_and_instances
result = conn.execute(querycontext.statement, self._params)
File "/usr/lib/python2.7/site-packages/SQLAlchemy-0.7.7-py2.7.egg/sqlalchemy/engine/base.py", line 1450, in execute
params)
File "/usr/lib/python2.7/site-packages/SQLAlchemy-0.7.7-py2.7.egg/sqlalchemy/engine/base.py", line 1583, in _execute_clauseelement
compiled_sql, distilled_params
File "/usr/lib/python2.7/site-packages/SQLAlchemy-0.7.7-py2.7.egg/sqlalchemy/engine/base.py", line 1697, in _execute_context
context)
File "/usr/lib/python2.7/site-packages/SQLAlchemy-0.7.7-py2.7.egg/sqlalchemy/engine/base.py", line 1690, in _execute_context
context)
File "/usr/lib/python2.7/site-packages/SQLAlchemy-0.7.7-py2.7.egg/sqlalchemy/engine/default.py", line 331, in do_execute
cursor.execute(statement, parameters)
sqlalchemy.exc.OperationalError: (OperationalError) no such table: teams u'SELECT teams.id AS teams_id, teams.name AS teams_name, teams.flag AS teams_flag, teams.url AS teams_url \nFROM teams' ()
It says it can't find the table teams- confirm with SqlLite ( I use firefox plugin) that it exists and that is the name. Also, copy the SQL in the error message in a Sqlite window and see if it runs.

Resources