[sqlalchemy] What is scalar-holding attribute?

2014-02-10 Thread Bao Niu
Could someone help give a simple definition of scalar-holding attribute? In
documentation for many to one relationship this term occurred, I tried
Google the term but didn't find a definition. Thx.

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.


Re: [sqlalchemy] What is the point to re-define each and every column in __init__ in a class?

2014-02-10 Thread Simon King
On Mon, Feb 10, 2014 at 7:13 AM, Bao Niu niuba...@gmail.com wrote:
 I'm new to sqlalchemy. I tried to search this question but didn't come up
 with accurate search terms. So I come here for some help from real people
 instead of search engine. For the following code:

 class Places(Base):

 

 __tablename__ = 'moz_places'



 id = Column(Integer, primary_key=True)

 url = Column(String)

 title = Column(String)

 rev_host = Column(String)

 visit_count = Column(Integer)

 hidden = Column(Integer)

 typed = Column(Integer)




 #--

 def __init__(self, id, url, title, rev_host, visit_count,

  hidden, typed):

 

 self.id = id

 self.url = url

 self.title = title

 self.rev_host = rev_host

 self.visit_count = visit_count

 self.hidden = hidden

 self.typed = typed


 If I already defined each column names as class variable,  why do I still
 need to re-define each of them as instance variables? I just can't
 understand the reason. Any hint would be highly appreciated. Thanks.


The assigments inside your __init__ function aren't really defining
instance variables. They are setting the values based on the
parameters passed to the constructor. Python doesn't automatically
initialise instance variables you - you need to do it yourself.

*However*, when you are using the declarative extension to map your
classes, you get a default __init__ method automatically, which will
perform those assignments for you. So as long as you use this
extension, you don't need to write trivial__init__ methods. See the
note at

  
http://docs.sqlalchemy.org/en/rel_0_9/orm/tutorial.html#create-an-instance-of-the-mapped-class


Hope that helps,

Simon

-- 
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.


[sqlalchemy] problems with mysql reflect

2014-02-10 Thread robert rottermann

Hi there,

I have a mysql database that I use on several linux boxes.
No I get the following error on on of them:

Traceback (most recent call last):
  File ../bin/zopepy, line 317, in module
execfile(__file__)
  File t.py, line 38, in module
BaseA.metadata.reflect(local_engine_a)
  File 
/home/zope/key2go/eggs/SQLAlchemy-0.9.2-py2.6-linux-i686.egg/sqlalchemy/sql/schema.py, 
line 3268, in reflect

Table(name, self, **reflect_opts)
  File 
/home/zope/key2go/eggs/SQLAlchemy-0.9.2-py2.6-linux-i686.egg/sqlalchemy/sql/schema.py, 
line 350, in __new__

table._init(name, metadata, *args, **kw)
  File 
/home/zope/key2go/eggs/SQLAlchemy-0.9.2-py2.6-linux-i686.egg/sqlalchemy/sql/schema.py, 
line 423, in _init

self._autoload(metadata, autoload_with, include_columns)
  File 
/home/zope/key2go/eggs/SQLAlchemy-0.9.2-py2.6-linux-i686.egg/sqlalchemy/sql/schema.py, 
line 435, in _autoload

self, include_columns, exclude_columns
  File 
/home/zope/key2go/eggs/SQLAlchemy-0.9.2-py2.6-linux-i686.egg/sqlalchemy/engine/base.py, 
line 1160, in run_callable

return callable_(self, *args, **kwargs)
  File 
/home/zope/key2go/eggs/SQLAlchemy-0.9.2-py2.6-linux-i686.egg/sqlalchemy/engine/default.py, 
line 345, in reflecttable

return insp.reflecttable(table, include_columns, exclude_columns)
  File 
/home/zope/key2go/eggs/SQLAlchemy-0.9.2-py2.6-linux-i686.egg/sqlalchemy/engine/reflection.py, 
line 463, in reflecttable
for col_d in self.get_columns(table_name, schema, 
**table.dialect_kwargs):

TypeError: get_columns() keywords must be strings



this is the testscript I use to create the error:


from sqlalchemy import create_engine
from sqlalchemy.dialects import mysql

import sys

infoa = {
'username' : 'root',
'pw' : '',
'host' :'localhost',
'drivername' : 'mysql',
'database' : 'energie_2',
'extra_param' : ''
}

LOCAL_DSN_A 
=%(drivername)s://%(username)s@%(host)s/%(database)s?charset=utf8%(extra_param)s 
% infoa


local_engine_a = create_engine(LOCAL_DSN_A)

BaseA.metadata.reflect(local_engine_a)


thanks for your help
and thanks for a wonderful library.

robert

--
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.


Re: [sqlalchemy] What is scalar-holding attribute?

2014-02-10 Thread Michael Bayer
a scalar is a value that is just one thing, as opposed to a collection.

e.g.:

x = 5   # — scalar

x = [1, 2, 3]  # — not a scalar

x = MyObject()   # — scalar object (SQLA distinguishes here between object and 
non…depending on what doc you’re looking at)

x = set([MyObject(), MyObject()])  # — not a scalar




On Feb 10, 2014, at 4:54 AM, Bao Niu niuba...@gmail.com wrote:

 Could someone help give a simple definition of scalar-holding attribute? In 
 documentation for many to one relationship this term occurred, I tried Google 
 the term but didn't find a definition. Thx.
 
 
 -- 
 You received this message because you are subscribed to the Google Groups 
 sqlalchemy group.
 To unsubscribe from this group and stop receiving emails from it, send an 
 email to sqlalchemy+unsubscr...@googlegroups.com.
 To post to this group, send email to sqlalchemy@googlegroups.com.
 Visit this group at http://groups.google.com/group/sqlalchemy.
 For more options, visit https://groups.google.com/groups/opt_out.



signature.asc
Description: Message signed with OpenPGP using GPGMail


Re: [sqlalchemy] problems with mysql reflect

2014-02-10 Thread Michael Bayer
so this code runs fine on many machines, just one machine is doing this?   have 
there been modifications made to the SQLAlchemy library on that one machine?  
I’m not able to reproduce this case.   There’s a path where 
table.dialect_kwargs gets populated and within the scope of MySQL reflection, 
the keys are definitely strings.  I’ve tried with python 2.6 just in case it’s 
a 2.6 issue.

if the code works on all machines except one then you need to isolate what is 
different on that one machine.

In particular, if you ran this code:


try:
BaseA.metadata.reflect(local_engine_a)
except:
import pdb
pdb.post_mortem()

assuming console script, this would drop you into a pdb session.  if you then 
type:

pdb  table.dialect_kwargs

it will show you the contents of the dictionary.  send me that.





On Feb 10, 2014, at 11:11 AM, robert rottermann rob...@redcor.ch wrote:

 Hi there,
 
 I have a mysql database that I use on several linux boxes.
 No I get the following error on on of them:
 
 Traceback (most recent call last):
  File ../bin/zopepy, line 317, in module
execfile(__file__)
  File t.py, line 38, in module
BaseA.metadata.reflect(local_engine_a)
  File 
 /home/zope/key2go/eggs/SQLAlchemy-0.9.2-py2.6-linux-i686.egg/sqlalchemy/sql/schema.py,
  line 3268, in reflect
Table(name, self, **reflect_opts)
  File 
 /home/zope/key2go/eggs/SQLAlchemy-0.9.2-py2.6-linux-i686.egg/sqlalchemy/sql/schema.py,
  line 350, in __new__
table._init(name, metadata, *args, **kw)
  File 
 /home/zope/key2go/eggs/SQLAlchemy-0.9.2-py2.6-linux-i686.egg/sqlalchemy/sql/schema.py,
  line 423, in _init
self._autoload(metadata, autoload_with, include_columns)
  File 
 /home/zope/key2go/eggs/SQLAlchemy-0.9.2-py2.6-linux-i686.egg/sqlalchemy/sql/schema.py,
  line 435, in _autoload
self, include_columns, exclude_columns
  File 
 /home/zope/key2go/eggs/SQLAlchemy-0.9.2-py2.6-linux-i686.egg/sqlalchemy/engine/base.py,
  line 1160, in run_callable
return callable_(self, *args, **kwargs)
  File 
 /home/zope/key2go/eggs/SQLAlchemy-0.9.2-py2.6-linux-i686.egg/sqlalchemy/engine/default.py,
  line 345, in reflecttable
return insp.reflecttable(table, include_columns, exclude_columns)
  File 
 /home/zope/key2go/eggs/SQLAlchemy-0.9.2-py2.6-linux-i686.egg/sqlalchemy/engine/reflection.py,
  line 463, in reflecttable
for col_d in self.get_columns(table_name, schema, **table.dialect_kwargs):
 TypeError: get_columns() keywords must be strings
 
 
 
 this is the testscript I use to create the error:
 
 
 from sqlalchemy import create_engine
 from sqlalchemy.dialects import mysql
 
 import sys
 
 infoa = {
'username' : 'root',
'pw' : '',
'host' :'localhost',
'drivername' : 'mysql',
'database' : 'energie_2',
'extra_param' : ''
 }
 
 LOCAL_DSN_A 
 =%(drivername)s://%(username)s@%(host)s/%(database)s?charset=utf8%(extra_param)s
  % infoa
 
 local_engine_a = create_engine(LOCAL_DSN_A)
 
 BaseA.metadata.reflect(local_engine_a)
 
 
 thanks for your help
 and thanks for a wonderful library.
 
 robert
 
 -- 
 You received this message because you are subscribed to the Google Groups 
 sqlalchemy group.
 To unsubscribe from this group and stop receiving emails from it, send an 
 email to sqlalchemy+unsubscr...@googlegroups.com.
 To post to this group, send email to sqlalchemy@googlegroups.com.
 Visit this group at http://groups.google.com/group/sqlalchemy.
 For more options, visit https://groups.google.com/groups/opt_out.



signature.asc
Description: Message signed with OpenPGP using GPGMail


[sqlalchemy] declarative polymorphic inheritance abstraction problem

2014-02-10 Thread Chris Withers

Hi All,

I'm trying to be efficient with my code, but it seems to tripping 
SQLAlchemy up.


So, basically I want to have a schema with two tables, content and 
project, to represent two three types of object:


- article has a set of fields as found in the 'content' table
- project has fields that are the set union of the 'content' and 
'project' tables' columns
- content is a plain content type, like article, and will likely never 
be instantiated, but just in case...
- in future, I may want to add more tables/content types where the 
fields are like project; a set union of the 'content' and 'project' 
tables' columns


So, I tried to be clever and start off with the following content 
class/table:


class Content(Base):

content_type = Column(String(20))

@declared_attr
def id(cls):
if cls.__name__=='Content':
return Column(Integer, primary_key=True)
else:
return Column(Integer, ForeignKey('content.id'), 
primary_key=True)


@declared_attr
def __tablename__(cls):
return cls.__name__.lower()

@declared_attr
def __mapper_args__(cls):
args = dict(polymorphic_identity = cls.__name__.lower())
if cls.__name__=='Content':
args['polymorphic_on'] = cls.content_type
return args

...with the following two classes associated with it:

class Article(Content):

@declared_attr
def __tablename__(cls):
return None

class Project(Content):

currency  = Column(String(3))
target = Column(Integer)
current = Column(Integer)

...but I get the following on import:

project.py, line 5, in module
class Project(Content):
  File 
/Users/chris/buildout-eggs/SQLAlchemy-0.9.1-py2.7-macosx-10.5-x86_64.egg/sqlalchemy/ext/declarative/api.py, 
line 53, in __init__

_as_declarative(cls, classname, cls.__dict__)
  File 
/Users/chris/buildout-eggs/SQLAlchemy-0.9.1-py2.7-macosx-10.5-x86_64.egg/sqlalchemy/ext/declarative/base.py, 
line 322, in _as_declarative

mt.map()
  File 
/Users/chris/buildout-eggs/SQLAlchemy-0.9.1-py2.7-macosx-10.5-x86_64.egg/sqlalchemy/ext/declarative/base.py, 
line 405, in map

**mapper_args
  File string, line 2, in mapper
  File 
/Users/chris/buildout-eggs/SQLAlchemy-0.9.1-py2.7-macosx-10.5-x86_64.egg/sqlalchemy/orm/mapper.py, 
line 593, in __init__

self._configure_inheritance()
  File 
/Users/chris/buildout-eggs/SQLAlchemy-0.9.1-py2.7-macosx-10.5-x86_64.egg/sqlalchemy/orm/mapper.py, 
line 900, in _configure_inheritance

self.local_table)
  File string, line 2, in join_condition
  File 
/Users/chris/buildout-eggs/SQLAlchemy-0.9.1-py2.7-macosx-10.5-x86_64.egg/sqlalchemy/sql/selectable.py, 
line 651, in _join_condition

between '%s' and '%s'.%s % (a.description, b.description, hint))
sqlalchemy.exc.NoForeignKeysError: Can't find any foreign key 
relationships between 'content' and 'project'.


If I change the id method to be decorated by classproperty rather than 
declared_attr, the error changes to:


article.py, line 3, in module
from .content import Content
content.py, line 7, in module
class Content(Base):
  File 
/Users/chris/buildout-eggs/SQLAlchemy-0.9.1-py2.7-macosx-10.5-x86_64.egg/sqlalchemy/ext/declarative/api.py, 
line 53, in __init__

_as_declarative(cls, classname, cls.__dict__)
  File 
/Users/chris/buildout-eggs/SQLAlchemy-0.9.1-py2.7-macosx-10.5-x86_64.egg/sqlalchemy/ext/declarative/base.py, 
line 322, in _as_declarative

mt.map()
  File 
/Users/chris/buildout-eggs/SQLAlchemy-0.9.1-py2.7-macosx-10.5-x86_64.egg/sqlalchemy/ext/declarative/base.py, 
line 405, in map

**mapper_args
  File string, line 2, in mapper
  File 
/Users/chris/buildout-eggs/SQLAlchemy-0.9.1-py2.7-macosx-10.5-x86_64.egg/sqlalchemy/orm/mapper.py, 
line 599, in __init__

self._configure_pks()
  File 
/Users/chris/buildout-eggs/SQLAlchemy-0.9.1-py2.7-macosx-10.5-x86_64.egg/sqlalchemy/orm/mapper.py, 
line 1189, in _configure_pks

(self, self.mapped_table.description))
sqlalchemy.exc.ArgumentError: Mapper Mapper|Content|content could not 
assemble any primary key columns for mapped table 'content'


What's going wrong here?

cheers,

Chris

--
Simplistix - Content Management, Batch Processing  Python Consulting
- http://www.simplistix.co.uk

--
You received this message because you are subscribed to the Google Groups 
sqlalchemy group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to sqlalchemy+unsubscr...@googlegroups.com.
To post to this group, send email to sqlalchemy@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.


Re: [sqlalchemy] declarative polymorphic inheritance abstraction problem

2014-02-10 Thread Michael Bayer

On Feb 10, 2014, at 2:36 PM, Chris Withers ch...@simplistix.co.uk wrote:

 Hi All,
 
 I'm trying to be efficient with my code, but it seems to tripping SQLAlchemy 
 up.
 
 So, basically I want to have a schema with two tables, content and project, 
 to represent two three types of object:

if you poke around with print statements or pdb, you can see that the “id” 
declared_attr is run only once, for Content.   this is because Content is 
mapped and your “id” declared_attr function is replaced with the mapped 
attribute “id”.

If you make a mixin like HasId and put it there, it still doesn’t work.   
Declarative finds the HasId mixin and the “id” function, but then when it 
resolves what “Project.id” really is, it again just calls getattr() and doesn’t 
call your function.   

Why’s it like that?  I don’t know, but I’m 99% sure at least three tests will 
fail if I change it, lets try:

Code like this:

class HasId(object):
@declared_attr
def id(cls):
if cls.__name__=='Content':
return Column('id', Integer, primary_key=True)
else:
return Column('id', Integer, ForeignKey('content.id'), 
primary_key=True)

class Content(HasId, Base):

content_type = Column(String(20))

@declared_attr
def __tablename__(cls):
return cls.__name__.lower()

class Project(Content):
currency  = Column(String(3))
target = Column(Integer)
current = Column(Integer)

so lets tell declarative to pull off of HasId.id when it scans “Project”, 
instead of getattr(Project, “id”):

--- a/lib/sqlalchemy/ext/declarative/base.py
+++ b/lib/sqlalchemy/ext/declarative/base.py
@@ -126,7 +126,7 @@ def _as_declarative(cls, classname, dict_):
 on declarative mixin classes.)
 elif isinstance(obj, declarative_props):
 dict_[name] = ret = \
-column_copies[obj] = getattr(cls, name)
+column_copies[obj] = getattr(base, name)
 if isinstance(ret, (Column, MapperProperty)) and \
 ret.doc is None:
 ret.doc = obj.__doc__

your test case works, but lets see the tests - six errors and four failures: 
https://gist.github.com/zzzeek/8925277

So we can’t do that.You know I love the mixins/declared_attr thing, but 
it’s hard.  There’s craploads of tests for it and the amount of decisions we’ve 
packed in there is already pretty intricate.

There’s some other variants that reduce the number of test failures, like this:

diff --git a/lib/sqlalchemy/ext/declarative/base.py 
b/lib/sqlalchemy/ext/declarative/base.py
index 4fda9c7..b74809e 100644
--- a/lib/sqlalchemy/ext/declarative/base.py
+++ b/lib/sqlalchemy/ext/declarative/base.py
@@ -126,7 +126,7 @@ def _as_declarative(cls, classname, dict_):
 on declarative mixin classes.)
 elif isinstance(obj, declarative_props):
 dict_[name] = ret = \
-column_copies[obj] = getattr(cls, name)
+column_copies[obj] = obj.__get__(obj, cls)
 if isinstance(ret, (Column, MapperProperty)) and \
 ret.doc is None:
 ret.doc = obj.__doc__


but still lots of failures; there’s a lot of use cases that just expect 
@declared_attr to hit once, then replace itself on the mapped class.

So the next idea is to just make a whole different command to do this, I 
sketched that up here:  www.sqlalchemy.org/trac/ticket/2952  

but I’m not really sure what I want to do with that.   mixins work great for 
single classes but within a hierarchy not so much, and adding more options 
makes me worried that we’re just adding flags rather than fixing the central 
idea somehow.  




signature.asc
Description: Message signed with OpenPGP using GPGMail