Showing posts with label mysql. Show all posts
Showing posts with label mysql. Show all posts

Friday, October 26, 2012

Two Foreign Keys to same model require related_name definition

When creating a new model that requires two foreign keys to refer to the same other model, django requires you to define the related_name attribute for the ForeignKey field.

The related_name attribute specifies the name of the reverse relation from the User model back to your model.
If you don't specify a related_name, Django automatically creates one using the name of your model and the suffix _set.

Thus unless you specify a unique related_name for at least one of your FKs, your models won't validate throwing an error:
Accessor for field 'primary' clashes with related field '<FKModelname>.<modelname_set>'

 Ref: http://stackoverflow.com/questions/2642613/what-is-the-related-name-mean-in-django/2642645#2642645

Tuesday, September 25, 2012

Avoid Cascading-deletes in Django-Admin

If you try deleting an object referenced by a Foreign Key or OneToOneField in Django Admin, it will attempt to do a cascade delete on all the related objects. This is documented here.

To avoid this, you need to set null = True and on_delete=models.SET_NULL as attributes for the Foreign Key like so:
user = models.ForeignKey(User, blank=True, null=True, on_delete=models.SET_NULL)

Ref: http://stackoverflow.com/a/6963556/1415352

Wednesday, August 15, 2012

Creating a Sproc/Function in your mysql db

I kept getting  You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line x.

I was trying to run the sql to create the sproc via a txt/sql file and then via terminal do a
$mysql -u root -p < sproc_create_filename.sql

Turns out, you need to have a DELIMITER when doing this. This is how I got it to finally work..


DELIMITER //
CREATE DEFINER = `username`@`%` PROCEDURE `sp_get_mainpage_data`(lpIntCustomerId INT)
BEGIN
-- , lpIntLocationId INT, lpStrLoanOfficerCode VARCHAR(48), lpIntDecision TINYINT, lpDateInitialScoreDate DATETIME,  lpDateEndScoreDate DATETIME

.
.
.
. and (shi.id IS NOT NULL OR (sbjasmt.repeated = 'y' OR sbjasmt.status = 'incomplete'));
END//

Just adding the DELIMITER // and END// sorted out the issue.

Monday, July 16, 2012

Set up SquirrelSql for Ubuntu

This is a guide to setting up SquirrelSql on Ubuntu..

1. Download SquirrelSql from : http://sourceforge.net/projects/squirrel-sql/files/1-stable/3.3.0/squirrel-sql-3.3.0-install.jar/download

2. Unzip the tarball by doing $ tar zxf <path-to-tarball>. To install the jar, $ java -jar <path-to-jar-file> . Then follow the on screen instructions (make sure to check MySql among the checkboxes) and you're done.

3. To connect to the database, you need a mysql driver. Download it from : http://dev.mysql.com/downloads/connector/j/

4. Fire up SquirrelSql and choose the "Drivers" on the vertical tab on the left.

5. Scroll to MySql and double click on it. Click on the 'Extra Class path' tab.

6. Click 'Add' and browse to the .jar friver file you downloaded at step3. Then hit 'Ok'.

7. Next, choose 'Aliases' on the vertical tab on the left and hit the '+' sign to add a new Alias.

8. Enter a name of your choice for the alias and in the driver dropdown menu select MySql.

9. For the url, enter the url that points to the database. For my local copy of the database I put. jdbc:mysql://localhost

10. Enter the username and password that you configured for your local db 'efldbadmin' and whatever password you choose. If connecting to staging use the password from dev.py

11. Hit 'Test' and then 'Connect'.  You should get a 'Connection Successful' message.

You're done! ^_^

Thursday, June 21, 2012

One stop guide: Get MySQL running on Ubuntu!!

This tutorial explains the process pretty well. Also highlighted in it are the errors I faced during the process.. http://codeinthehole.com/writing/how-to-set-up-mysql-for-python-on-ubuntu/
$ sudo apt-get install python-dev libmysqlclient-dev
$ pip install MySQL-python

This should let you run the staging database.

For your local mysql databses,
$ sudo apt-get install mysql-server

Then..
mysql -u root -p
>enter password

mysql> CREATE USER 'efldbadmin'@'localhost' IDENTIFIED BY 'some_pass';
mysql> GRANT ALL PRIVILEGES ON *.* TO 'efldbadmin'@'%' WITH GRANT OPTION;

CREATE DATABASE stg_mullet;

Import sql file into the db:


mysql -u efldbadmin -p stg_mullet < mullet_2012-05-25.sql
Enter password:

DONE!

Popular Posts