AttributeError: 'Field parent_id not found in browse_record(hr.employee, 6) - openerp-7

I cant access employee table ie hr_employee table fields from my new module.
It shows error as
AttributeError: 'Field parent_id not found in browse_record(hr.employee, 6)
Any one please help!!!
Anu

changing code to:
resource_id = self.pool.get('resource.resource').search(cr, uid, [('user_id','=', uid)][0]
Emp_id = self.pool.get('hr.employee').search(cr, uid, [('resource_id','=', resource_id)][0]
corrected my issue.

Related

Try/Except not working when fetching from SQLite database

I am bulding an app that extracts different attributes from an XML file in my iTunes library export. Since not every song has a genre, finding one per song will not always work. When the program inserts a genre into the 'Genre', it creates an automatic id
CREATE TABLE Genre (
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
name TEXT UNIQUE
);
The thing is, when there is no 'Genre' found in the XML file, nothing is inserted, meaning I cannot later fetch the id created by the addition of something to put it in a tabke in order to do relational queries. To stop this, I put the insert and fetch inside a try/except that would catch the
TypeError: 'NoneType' object is not subscriptable
error when it is unable to
cur.fetchone()[0]
. In the except I established the genre as "No Genre" to generate the unique id for the case that there were no genre found. This, even though it catches an exception in the try block, isnt running. Error:
TypeError Traceback (most recent call last)
~\Documents\python\tracks.py in <module>
83 cur.execute('SELECT id FROM Artist WHERE name = ? ', (genre, ))
---> 84 genre_id = cur.fetchone()[0]
85 except:
TypeError: 'NoneType' object is not subscriptable
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
~\Documents\python\tracks.py in <module>
87 cur.execute('''INSERT OR IGNORE INTO Genre (name) VALUES ( ? )''', ( genre, ) )
88 cur.execute('SELECT id FROM Artist WHERE name = ? ', (genre, ))
---> 89 genre_id = cur.fetchone()[0]
90
91
TypeError: 'NoneType' object is not subscriptable
Help! Why isn't the try/except not working?
try:
cur.execute('''INSERT OR IGNORE INTO Genre (name) VALUES ( ? )''', ( genre, ) )
cur.execute('SELECT id FROM Artist WHERE name = ? ', (genre, ))
genre_id = cur.fetchone()[0]
except:
genre = "No Genre"
cur.execute('''INSERT OR IGNORE INTO Genre (name) VALUES ( ? )''', ( genre, ) )
cur.execute('SELECT id FROM Artist WHERE name = ? ', (genre, ))
genre_id = cur.fetchone()[0]
Okay, solution first
import sqlite3
conn = sqlite3.connect('systeminfo.db')
cur = conn.cursor()
genre = 'test'
try:
cur.execute('''INSERT OR IGNORE INTO Genre (name) VALUES ( ? )''', ( genre, ) )
cur.execute('SELECT id FROM Artist WHERE name = ? ', (genre, ))
genre_id = cur.fetchone()
if genre_id is None:
print('Select did not find any artist for {} genre'.format(genre))
else:
print('Select resulted in Artist ID {}'.format(genre_id[0]))
except Exception as e:
print('Exception: {}'.format(e))
raise Exception(e)
conn.commit()
conn.close()
See, there's a possibility that cur.fetchone() can result in a row or None if there is no row. So, let's do an if..then to check for None.
Here's what seems to be happening. Scroll way at the bottom of the post to find your answer.
Table
CREATE TABLE Artist (
id integer not null primary key autoincrement,
name text,
genre text
);
CREATE TABLE Genre (
id integer not null primary key autoincrement,
name text unique
);
Data
sqlite> select * from Artist;
Run Time: real 0.000 user 0.000086 sys 0.000054
sqlite> select * from Genre;
Run Time: real 0.000 user 0.000092 sys 0.000064
sqlite>
Basically, there's no data.
Your code
import sqlite3
conn = sqlite3.connect('systeminfo.db')
cur = conn.cursor()
genre = 'test'
try:
cur.execute('''INSERT OR IGNORE INTO Genre (name) VALUES ( ? )''', ( genre, ) )
cur.execute('SELECT id FROM Artist WHERE name = ? ', (genre, ))
genre_id = cur.fetchone()[0]
except:
cur.execute('''INSERT OR IGNORE INTO Genre (name) VALUES ( ? )''', ( genre, ) )
cur.execute('SELECT id FROM Artist WHERE name = ? ', (genre, ))
genre_id = cur.fetchone()[0]
conn.commit()
conn.close()
Issue
Your errors are happening in 2 places. In your try block, there's an error at genre_id = cur.fetchone()[0]. Once the error is hit, the control goes to except block. In that block, the code is repeated. That means, the error is repeated. In that block, there is no error handling because it IS error handling. So, python throws another error in except block for the same thing genre_id = cur.fetchone()[0].
Clean up the issue
import sqlite3
conn = sqlite3.connect('systeminfo.db')
cur = conn.cursor()
genre = 'test'
try:
cur.execute('''INSERT OR IGNORE INTO Genre (name) VALUES ( ? )''', ( genre, ) )
cur.execute('SELECT id FROM Artist WHERE name = ? ', (genre, ))
genre_id = cur.fetchone()[0]
except Exception as e:
print('Exception: {}'.format(e))
conn.commit()
conn.close()
Alright. In except we want to handle the exception and perhaps print out the exception. So, we will use except Exception as e. Now, error information is in e. We print that out and that's it.
$ python myfile.py
Exception: 'NoneType' object is not subscriptable
But what if I want to show where the error is?
Add raise Exception(e) right under print('Exception: {}'.format(e)). So, the result becomes this:
$ python myfile.py
Exception: 'NoneType' object is not subscriptable
Traceback (most recent call last):
File "testy.py", line 9, in <module>
genre_id = cur.fetchone()[0]
TypeError: 'NoneType' object is not subscriptable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "testy.py", line 12, in <module>
raise Exception(e)
Exception: 'NoneType' object is not subscriptable
That raise statement lists out on the screen where issue happened.

Update a Table using a Join

I wish to update a table using, but need to use another table to get the correct field. The new information is not taken from another field from another table.
The following SQL statement returns the correct information:
SELECT PURCHASEHEADER.ORDERNOTES
FROM PURCHASEHEADER, ASSEMBLYLINESOURCE
WHERE ASSEMBLYLINESOURCE.HEADERSYSUNIQUEID = 72637001
AND PURCHASEHEADER.ORDERNUMBER = ASSEMBLYLINESOURCE.PURCHASEORDERNUMBER
I have tried the following:
UPDATE PURCHASEHEADER SET PURCHASEHEADER.ORDERNOTES = 'Updated'
WHERE EXISTS (
SELECT 1 FROM ASSEMBLYLINESOURCE
WHERE PURCHASEHEADER.ORDERNUMBER = ASSEMBLYLINESOURCE.PURCHASEORDERNUMBER
) AND ASSEMBLYLINESOURCE.HEADERSYSUNIQUEID = 72637001
An error is returned saying: " ...Column Unknown ASSEMBLYLINESOURCE.HEADERSYSUNIQUEID..." but it does exist as it works in the first query.
I have seen similar posts from Mark Rotteveel dated July 2017, but still can't get it to work.
There is an issue with your closing bracket. Try this, it worked for me.
UPDATE PURCHASEHEADER set PURCHASEHEADER.ORDERNOTES = 'Updated'
WHERE EXISTS (SELECT 1 FROM ASSEMBLYLINESOURCE WHERE
PURCHASEHEADER.ORDERNUMBER = ASSEMBLYLINESOURCE.PURCHASEORDERNUMBER AND
ASSEMBLYLINESOURCE.HEADERSYSUNIQUEID = 72637001)

Fetching the complete 'date' field was not coming form 'hr.employee' model

I am trying to get the data from 'date' field that 'date' field is in 'hr.employee' to 'hr.payslip'.I create a function for that task.
Code:
class employee_datecheck(models.Model):
_inherit = 'hr.payslip'
#api.onchange('employee_id')
#api.constrains('employee_id')
def date_check(self):
if self.employee_id:
self.t2 = self.date_from
self.tax
product_obj = self.env['hr.employee'].search([('name' , '=' ,self.employee_id.name)])
if product_obj:
for products in product_obj:
product_new_obj = self.env['hr.employee'].browse([products.id])
for tax in product_new_obj.joindate:
raise Warning(self.tax)
problem is:
The date was not fetching properly i.e it just showing the '2' instead of '2017-09-21'.Please help me.
I think you could try to print directly the joindate without doing a loop:
for products in product_obj:
product_new_obj = self.env['hr.employee'].browse([products.id])
self.tax = product_new_obj.joindate:
raise Warning(self.tax)
I hope this help you.
phani,
1) You code does not look from v8 as tagged on the question, code is v9+ new API.
2) Not sure why are you searching on Model product and employee because hr.playslp Modle itself has employee field employee_id that you can use to get the date. Also, you should not use onchange and constrains together
Below is sample code:
class employee_datecheck(models.Model):
_inherit = 'hr.payslip'
#api.onchange('employee_id')
def date_check(self):
if self.employee_id:
raise Warning(self.employee_id.joindate)
It will be good if you can give more detail for an accurate answer.
Bests

Creating an XML document through SQL, but I get syntax error

I have a small query to make an XML document:
Select XMLELEMENT(NAME movie,
XMLFOREST(Movie.name as title, year as year, rating as rating, plot_outline as plot,
XMLELEMENT(NAME actor, 'actor',
XMLAGG(
XMLELEMENT(NAME role, Acts.role )
),
XMLAGG(
XMLELEMENT(NAME person, Person.name))
)))
as result from Movie, Acts, Person
where Movie.mid = Acts.mid
and Acts.pid = Person.pid
But I get this error:
ERROR: unnamed XML element value must be a column reference
LINE 3: XMLELEMENT(NAME actor, 'actor',
^
This was supposed to be my expected result in XML:
<movie>
<title>Godfather, The</title>
<year>1972</year>
<rating>9.0</rating>
<plot>A Mafia boss' son, previously uninvolved in the business, takes over when his father is critically wounded in a mob hit.</plot>
<actor>
<role>Don Vito Corleone</role>
<person>Robert De Niro</person>
</actor>
<actor>
<role>Someone else</role>
<person>Someone else</person>
</actor>
</movie>
How do I get rid of this error message?
1) You can omit "name" word in xmlelement. xmlelment(name "name"... ) = xmlelment("name"...).
2) unnamed XML element value must be a column reference - that error is raise by xmlforest function.
xmlforest( col1, col2 as "TEST") - works fine
xmlforest(col1,xmlelement("TEST")) - throws error should be xmlforest(col1,xmlelement("TEST") as "xxx" )
3) XMLELEMENT(NAME actor, 'actor' you don need this. If i understand your query. You try to concatenate two xmlagg into one xmlelement. To do this use xmlconcat(xmlagg(..),xmlagg(..))
4) Finall query.
select xmlelement(
movie
, xmlforest(Movie.name as title
, year as year
, rating as rating
, plot_outline as plot
, xmlconcat(xmlagg(xmlelement(role, Acts.role)), xmlagg(xmlelement(person, Person.name))) as "actor"))
as result
from Movie, Acts, Person
where Movie.mid = Acts.mid and Acts.pid = Person.pid
You also have to add proper group by clause to it. Because it raise another exception. ORA-00937: not a single-group group function

Active Record query causing SQLException near "," syntax error

I'm trying to display all such rows of a table STUDENTS which have a value of an attribute, such as COURSE IN a set of given values e.g. 'MS', 'PhD'.
I get the values in the students_controller.rb file using params. I tried to run an Active Record query using where to do the job:
#all_courses = ['MS', 'PhD', 'BA', 'MSc']
#students = Student.where("course IN :courses" , {:courses => params.has_key?(:courses) ? params[:courses].keys : #all_courses})
But I get the following error:
SQLite3::SQLException: near ",": syntax error: SELECT "students".* FROM "students" WHERE (course IN 'MS', 'PhD', 'BA', 'MSc')
I think the error might be due to the absence of ; at the end of the SQL query generated by Active Record, but I cannot do anything to get that semicolon at the end.
You need to use parentheses: "course IN (:courses)"